Search code examples
sqloracle-databaseora-00905

How to fix ORA-00905 missing keyword with case expression


I have problem with case in the line I marked it below.

I need it to be shown like

debits | credits | total debits | total credits

The SQL query is:

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

Solution

  • There is a syntax error in the following two lines

    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
    

    the end should be with in the parentheses, so the query wil be:

    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