内部case语句在尝试对值求和时抛出错误

时间:2017-08-28 20:19:47

标签: sql sql-server

select sum(t.amount)
from
(select yearquarter,
       (case when sum(case when type not in (3, 4) then 1 else 0 end) = 0
             then sum(amount)
             else sum(case when type not in (3, 4) then amount else 0 end)
        end)
from t
where status = 'A'
group by yearquarter)t;

需要总结"金额"来自内部案例陈述,但获得错误

1 个答案:

答案 0 :(得分:1)

您没有在t子查询中命名amount列。

select sum(t.amount)
from
(select yearquarter,
       (case when sum(case when type not in (3, 4) then 1 else 0 end) = 0
             then sum(amount)
             else sum(case when type not in (3, 4) then amount else 0 end)
        end) AS amount
from t
where status = 'A'
group by yearquarter)t;