SELECT CustomerID
,COUNT(CASE WHEN TransactionType = 'ESL'
AND TransactionLocation = 'WBEE' THEN 1 ELSE NULL END) AS WBEEESL
,COUNT(CASE WHEN TransactionType = 'LOTE'
AND TransactionLocation = 'WBEE' THEN 1 ELSE NULL END) AS WBELOTE
,COUNT(CASE WHEN TransactionType = 'ESL'
AND TransactionLocation = 'VALE' THEN 1 ELSE NULL END) AS VALEESL
,COUNT(CASE WHEN TransactionType = 'LOTE'
AND TransactionLocation = 'VALE' THEN 1 ELSE NULL END) AS VALELOTE
FROM TABLE_NAME
GROUP BY CustomerID
SQL Script Transaction Log to Table Report
-
10-06-2023 - |
Question
I am trying to write a script that I can run to create a report that will look through a table (customer transaction log) and will count the number of times each customer uses each transaction type for each location. For report purposes I NEED only single line for each customer.
eg Transaction Table
CustomerID TransactionType TransactionLocation Timestamp
90214X ESL WBEE 20140301
90214X LOTE WBEE 20140301
90214X ESL WBEE 20140302
90214X LOTE VALE 20140303
90214X ESL WBEE 20140302
90214X ESL VALE 20140302
90633X LOTE WBEE 20140301
90633X ESL VALE 20140302
90678X ESL WBEE 20140303
report needs to be
CustomerID WBEEESL WBEELOTE VALEESL VALELOTE
90214X 3 1 1 1
90633X 0 1 1 0
90678X 1 0 0 0
I will be require to run the script on a regular basis and would like to create the script so I can automate the process.
(sorry table wouldnt layout nicely here)
Any help would be appreciated, thanks
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow