I would like to SUM up one column, but based on different transaction types, and have those sums appear in one row only.
My SQL (SQL Server 2000) looks like this:
SELECT c.customername,
CASE WHEN t.transactiontypekey IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19) THEN SUM(t.tranamount) ELSE 0 END as 'Tot-Exp',
CASE WHEN t.transactiontypekey IN (20,21,22,23,30,32,34,36) THEN SUM(t.tranamount) ELSE 0 END as 'Tot-Rev',
CASE WHEN t.transactiontypekey IN (31,33,35,37) THEN SUM(t.tranamount) ELSE 0 END as 'Tot-Fee'
FROM customers c, transactions t
WHERE c.customerkey = t.customerkey
GROUP BY c.customername, t.transactiontypekey
ORDER BY c.customername
In the above SQL, the transaction types are broken out into Expenses, Revenue and Fees and the output from above is showing as:
customernameTot-Exp Tot-Rev Tot-Fee
CUSTOMER1 13.3900 .0000 .0000
CUSTOMER1 .0000 549.0000 .0000
CUSTOMER1 .0000 .0000 60.0000
CUSTOMER1 .0000 .0000 .0000
How can I get my output to look like:
customername Tot-Exp Tot-Rev Tot-Fee
CUSTOMER1 13.390 549.00 60.000
one row per customer with all of the totals of the ‘t.tranamount’ column in that one row only?
Sum the
CASEexpression itself;And remove
t.transactiontypekeyfrom the theGROUP BY.