Hi @JM12377 ,
The syntax of these two methods is correct.
Method_1:
Case when WS_Cost_Code = 1 then SUM(WS_Extended) else 0 end
In the first method, when WS_Cost_Code = 1, the sum of all WS_Extended corresponding to WS_Cost_Code = 1 is returned, and 0 is returned for all WS_Cost_Code <>1.
Method_2:
SUM(case when WS_Cost_Code=1 then WS_Extended_Cost else 0 end )
In the second method, when WS_Cost_Code = 1, the value of WS_Extended is returned, and when WS_Cost_Code <> 1, it returns 0. Then the returned values are summed. If you specify the GROUP BY WS_Cost_Code clause, the returned result is the same as in the first method. If not specified, only one value is returned.
Below is the data I tested:
select * from t
select
case when No1 = 3 then SUM(No2) else 0 end m1
from t
group by No1
select
sum(case when No1=3 then No2 else 0 end ) m2
from t
select case when No1 = 3 then SUM(No2) else 0 end m1
,sum(case when No1=3 then No2 else 0 end ) m2
from t
group by No1
Output:
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".