我有
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_Citizens,它具有像 Table.Citizen_id 这样的输入参数和仅针对此 ID 的输出结果。但是如果 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
那行不通