如何使用 case 表达式修复 ORA-00905 缺少关键字

How to fix ORA-00905 missing keyword with case expression

我在下面标记的行中遇到 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

以下两行语法错误

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

你真的不需要 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 的聚合没有任何意义。