Reputation: 65
Could you please help me to make query in SQL Server 2012? I have got example data like in a table below.
code price
p1 1,00
p2 1,00
p3 1,00
p4 1,00
p1 2,00
p3 2,00
p2 4,00
I need to get sum(p1), sum(p2), sum(other) like
p1 3.00
p2 5.00
other 4.00
Thank you very much.
Upvotes: 0
Views: 31
Reputation: 408
You can use from case when in your query:
CASE WHEN code NOT IN ('p1', 'p2') THEN 'Other' ELSE code END
You obviously also need this as the expression in the GROUP BY:
SELECT
CASE WHEN code NOT IN ('p1', 'p2') THEN 'Other' ELSE code END AS NewCode,
SUM(price)
FROM TableName
GROUP BY CASE WHEN code NOT IN ('p1', 'p2') THEN 'Other' ELSE code END
Upvotes: 1