1

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
4

2 回答 2

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
于 2019-05-06T08:04:23.720 回答
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条款没有意义。并且聚合 bycv_amount并没有真正的意义。

于 2019-05-06T10:44:20.497 回答