如何使用大小写表达式修复ORA-00905缺少的关键字

时间:2019-05-06 07:44:01

标签: sql oracle ora-00905

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

2 个答案:

答案 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进行的聚合实际上没有任何意义。