我有一个像以下
的表格结构TNO DATE NETAMT CHQ CASH PARTY
---------------------------------------------
T1 01/04/2016 100 10 0 TEST1
T2 15/04/2016 50 0 0 TEST2
T3 18/04/2016 100 100 0 TEST3
现在我想制作一个SQL查询,它会给我一个老化的报告,说明哪些交易在哪些板块中表现突出。
我希望以下列格式看到结果
PARTY 0-3 DAYS 4-6 DAYS 7-15 DAYS
---------------------------------------------
TEST1 0 0 90
TEST2 0 50 0
TEST3 0 0 0
答案 0 :(得分:4)
试试这个
SELECT PARTY,
SUM(CASE WHEN DATEDIFF(DAY,[DATE],GETDATE()) BETWEEN 0 AND 3 THEN (NETAMT - CHQ) ELSE 0 END) AS [0_3_DAYS],
SUM(CASE WHEN DATEDIFF(DAY,[DATE],GETDATE()) BETWEEN 4 AND 6 THEN (NETAMT - CHQ) ELSE 0 END) AS [4_6_DAYS],
SUM(CASE WHEN DATEDIFF(DAY,[DATE],GETDATE()) BETWEEN 7 AND 15 THEN (NETAMT - CHQ) ELSE 0 END) AS [7_15_DAYS],
FROM TABLE1
GROUP BY PARTY