SQL脚本事务日志到表报告

时间:2014-03-05 22:49:24

标签: sql sql-scripts

我正在尝试编写一个脚本,我可以运行该脚本来创建一个查看表(客户事务日志)的报告,并计算每个客户为每个位置使用每种事务类型的次数。出于报告目的,我只需为每位客户提供单行服务。

例如交易表

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

报告需要

CustomerID  WBEEESL  WBEELOTE VALEESL VALELOTE

90214X      3        1        1       1

90633X      0        1        1       0

90678X      1        0        0       0

我将需要定期运行脚本,并希望创建脚本,以便我可以自动执行该过程。

(抱歉的表格在这里不能很好地布局)

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:0)

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