I’ve multiple columns which has the same value and it is showing incorrect results when I summarize them. Is there any way to write a case statement in sql, if column A >0 then make all remaining columns as 0 (column B, Column C, Column D= 0)
if column B >0 then make all remaining columns as 0 (column A, Column C, Column D= 0)
1st column gets the higher priority in this sequence.
I tried writing case statement but it did not work.
>Solution :
See if this example helps:
SELECT a
,CASE WHEN a > 0 THEN 0 ELSE b END AS b
,CASE WHEN a > 0 OR b > 0 THEN 0 ELSE c END AS c
,CASE WHEN a > 0 OR b > 0 OR c > 0 THEN 0 ELSE d END AS d
FROM tbl;