I’m trying to do a select statement using a “CASE” operation, but it says that my condition (abc.reason) is invalid because it is not contained in either an aggregate function or the GROUP BY clause. I’ve tried adding it to the GROUP BY clause but there is an unnecessary row created in my results table. Any tips?
SELECT
DISTINCT ID
,(CASE WHEN reason = 4 THEN null ELSE SUM(quantity*price) END) AS Value
,COUNT(*) AS CountAll
FROM TransactionsDB
GROUP BY ID
ORDER BY ID DESC
If I change my GROUP BY Clause to GROUP BY ID, reason then my results are:
ID —- VALUE —– COUNTALL
id1 – – 1000 – – – – – 22
id1 – – – NULL – – – – 1
id2 – – – – 232 – – — 17
id3 – – – – 113 — – – 2
id3 – – – – NULL – – 1
When really I need the results:
ID —- VALUE —– COUNTALL
id1 – – 1000 – – – – – 23
id2 – – – – 232 – – — 17
id3 – – – – 113 — – – 3
Thank you in advance!!!!
1 Answer