寻找一种避免错误消息的优雅方法 无法对包含聚合或子查询的表达式执行聚合函数。
SELECT AccountName
, SUM(CASE
WHEN CallFlag = 1 AND Attempts < c.nMaxAttempts
THEN 1
ELSE 0
END)CallRecords
, SUM(CASE
WHEN CRC IN(
SELECT szCRC COLLATE SQL_Latin1_General_CP1_CI_AS
FROM ContactCenter..CallDispositionMap
WHERE bBadRecord = 1)
THEN 1
ELSE 0
END)BadRecords
FROM Calls;
答案 0 :(得分:0)
您需要使用子选择。此外,您还需要在查询中使用Group BY
。试试这个,
SELECT
AccountName,
SUM(CallRecords) AS CallRecords,
SUM(BadRecords) AS BadRecords
FROM
(
SELECT AccountName
, (CASE
WHEN CallFlag = 1 AND Attempts < c.nMaxAttempts
THEN 1
ELSE 0
END)CallRecords
, (CASE
WHEN CRC IN(
SELECT szCRC COLLATE SQL_Latin1_General_CP1_CI_AS
FROM ContactCenter..CallDispositionMap
WHERE bBadRecord = 1)
THEN 1
ELSE 0
END)BadRecords
FROM Calls) AS data
GROUP BY AccountName