Reputation: 79
So i have this query that consist of 3 columns. First:
Select c1, sum(c2), sum(c3)
from t
where c4 not in ('xxx')
Group by c1
Then I get both sum(c2) and sum(c3) based on the condition.
I only want to apply the condition on sum(c3). How do I do that?
I tried below and it did not work:
Select c1, sum(c2), ( select sum(c3) from t where c4 not in ('xxx')) as c3
from t
group by c1
Any suggestions? Thanks!
Upvotes: 0
Views: 34
Reputation: 726599
You can use a CASE
expression with conditional aggregation to compute the c3
sum:
SELECT c1,
SUM(c2),
SUM(CASE WHEN c4 NOT IN ('xxx') THEN c3 ELSE 0 END)
FROM t
GROUP BY c1
Upvotes: 2
Reputation: 827
Try to use
Select c1, sum(c2), sum(case c4 when 'xxx' then 0 else c3 end) as c3
from t
group by c1
Upvotes: 0