I am trying to run a query in SQLite3 where I want the grand total of Savings column from Case statement. My SQL query looks like this now:
SELECT COUNT(PhoneNumber), Dept, Device
CASE
WHEN Device='Apple' THEN 500*COUNT(PhoneNumber)
WHEN Device='Samsung' THEN 400*COUNT(PhoneNumber)
ELSE 100*COUNT(PhoneNumber)
END AS 'Savings'
FROM TestTable
WHERE Active= 'No' OR Cancel=1
GROUP BY Dept, Device
I tried inserting SUM() in front of CASE statement but I am getting an error "Result: misuse of aggregate function count() At line 1:" Could anyone guide me on how to build this query?