总结案例陈述

时间:2015-09-16 21:34:50

标签: sql sql-server

以下SQL正在返回'无法对包含聚合或子查询的表达式执行聚合函数。',任何人都可以帮忙吗?

SELECT 
sum(case when Frequency = 'Monthly' then ISNULL(SUM(Amount),0.0) else 0 end) + 
sum(case when Frequency = '4 Weekly' then ISNULL(SUM(Amount),0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(SUM(Amount),0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly' then ISNULL(SUM(Amount),0.0) / 5 else 0 end) 
FROM TableWHERE Id = 1

3 个答案:

答案 0 :(得分:1)

如果您想要条件聚合,您只需要一个$request->headers->get("Authorization")

sum()

答案 1 :(得分:1)

我想你想做点什么

SELECT sum(case when Frequency = 'Monthly' then Amount else 0 end) + 
       sum(case when Frequency = '4 Weekly' then Amount / 2 else 0 end) +
       sum(case when Frequency = 'Fortnightly' then Amount / 3 else 0 end) +
       sum(case when Frequency = 'Weekly' then Amount,0.0) / 5 else 0 end) 
FROM Table
WHERE Id = 1;

答案 2 :(得分:1)

SELECT 
sum(case when Frequency = 'Monthly'     then ISNULL(Amount,0.0)     else 0 end) + 
sum(case when Frequency = '4 Weekly'    then ISNULL(Amount,0.0) / 2 else 0 end) +
sum(case when Frequency = 'Fortnightly' then ISNULL(Amount,0.0) / 3 else 0 end) +
sum(case when Frequency = 'Weekly'      then ISNULL(Amount,0.0) / 5 else 0 end) 
FROM Table
WHERE Id = 1