0

私は持っている

SELECT 
    Table.Citizen_id AS ID,
    Citizens.Name as Name,
    Citizens.City as City  
FROM Table 
INNER JOIN Citizens   
ON Table.Citizen_id = Citizens.id
GROUP BY Table.Citizen_id, Citizens.Name,Citizens.City;

Table.Citizen_id のような入力パラメーターを持ち、この ID のみの結果を出力する関数 Table_Citizens を作成します。ただし、Table.Citizen_id=0 の場合、関数はすべての ID のデータを出力する必要があります。そして、これを実行する方法を知りたいです。

CREATE FUNCTION dbo.Calls_ABCs(@ABC_id AS INT) 
RETURNS TABLE 
AS 
RETURN 
    SELECT 
    Calls.ABC_id AS ID,
    ABCs.Name as Name,
    ABCs.City as City, 
    COUNT(Calls.Call_start) as Count_Calls, 
    AVG(Calls.Duration) AS AVG_Call_duration,
    MAX(Calls.Duration) AS MAX_Call_duration,
    SUM(Calls.Duration) AS SUM_Call_duration,
    SUM(Calls.Cost) AS SUM_Call_cost,
    AVG(Calls.Cost) AS AVG_Calls_Cost,
FROM Calls 
INNER JOIN ABCs   
ON Calls.ABC_id = ABCs.id WHERE (@ABC_id=0 or Calls.ABC_id = @ABC_id) and  Calls.Call_Start > '2013-03-17'  GROUP BY Calls.ABC_id, ABCs.Name,ABCs.City;

GO 

それはうまくいきません

4

2 に答える 2

2

関数内のクエリは次のようになります。

SELECT 
    Table.Citizen_id AS ID,
    Citizens.Name as Name,
    Citizens.City as City  
FROM Table INNER JOIN
     Citizens   
     ON Table.Citizen_id = Citizens.id
where @Id = 0 or table.Citizen_id = @id
GROUP BY Table.Citizen_id, Citizens.Name,Citizens.City;

多くの場合、これはNULLではなく値で示されます。これは、すべてのデータ型で機能するためです0NULLその場合:

where @Id is NULL or table.Citizen_id = @Id
于 2013-05-27T13:21:59.850 に答える