我在下面标记的行中遇到case
的问题。
我需要像
一样显示debits | credits | total debits | total credits
SQL查询为:
SELECT cv_amount,
case when cv_amount > 0 then cv_amount else 0 end as debits,
case when cv_amount < 0 then cv_amount*-1 else 0 end as credits,
sum(case when cv_amount > 0 then cv_amount else 0)--- ERROR SHOWEN HERE--- end as d,
sum(case when cv_amount < 0 then cv_amount*-1 else 0) end as c
FROM dof
where currency_code = 368
AND to_DATE('05/05/19', 'DD/MM/YY') and to_DATE('05/05/19', 'DD/MM/YY')
group by cv_amount
答案 0 :(得分:0)
以下两行出现语法错误
sum(case when cv_amount > 0 then cv_amount else 0) end as d,
sum(case when cv_amount < 0 then cv_amount*-1 else 0) end as c
end
应该放在括号中,因此查询将是:
sum(case when cv_amount > 0 then cv_amount else 0 end) as d,
sum(case when cv_amount < 0 then cv_amount*-1 else 0 end) as c
答案 1 :(得分:0)
您实际上不需要case
:
select cv_amount,
greatest(cv_amount, 0) as debits,
abs(least(cv_amount, 0)) as credits,
sum(greatest(cv_amount, 0)) as d,
sum(abs(least(cv_amount, 0))) as c
from dof
where currency_code = 368 and
<something goes here> = date '2019-05-05'
group by cv_amount;
更大的问题是where
子句没有意义。 cv_amount
进行的聚合实际上没有任何意义。