mspahwa, what tonkuma gave you fixes your problem.
The situation is the CASE Expression has two formats. They are Simple When and Searched When.
The Simple When is what you are using. The syntax is:
CASE expression WHEN expression THEN result-expression ELSE result-expression END
With this format the CASE expression is compared to the WHEN expression. If it is Equal, then it is True. If it is Not Equal it is False and the When is skipped and either the next When (if any) is evaluated
The Searched When is what Tonkuma showed you. The syntax is:
CASE WHEN search-condition THEN result-expression ELSE result-expression END
With this format there is no Case expression that is used by the WHEN clauses. Every WHEN search-condition is evaluated only on the items associated with it. If all conditions evaluate out to True, the THEN result-expression is used. If they evaluate out to False, the When is skipped and either the next When (if any) is evaluated.
As an example, a Simple When has one 'value' and each WHEN is checked against that value and it can only be an equality check (equal-True, not equal-False). It is limited in what you can do.
Code:
CASE MONTH(CURRENT DATE)
WHEN 1 THEN ...
WHEN 2 THEN ...
WHEN 3 THEN ...
(etc.)
END
A Searched When can have a lot of flexibility in what you can do as you can use Boolean login (AND, OR, etc.).
Code:
CASE
WHEN (col1 = 'AA' AND col2 < 5) or (col3 between 1 and 10 and col4 like 'XYZ%' THEN ...
WHEN col5 >= 8 THEN ...
WHEN col6 < current data - 6 months THEN ...
(etc.)
END
As you can see, you are not limited to only Equal / Not equal comparison. And you don't even need to make the same type of comparisons in every WHEN (in my example, the first When checks col1-col4, the second When check col5 and the third When checks col6.
Getting back to your problem, this line:
Code:
CASE (a.CODE) when 'D8C1C340' or 'D8C9E240' then sum(f.AMOUNT)
has a Simple When format
CASE A.CODE
but the WHEN has multiple values it is checking (You can only compare one item)
WHEN 'D8C1C340' or 'D8C9E240'
Tonkuma gave you 3 ways to fix it.
A) keep your Simple When by moving the multiple items to separate WHEN clause.
B & C) change the Simple When to a Searched When and use either an Or or In to compare for the multiple items.