INPUT:
| ID | Class | SubDate | RecDate | SubAmt| RecAmt|
--------------------------------------------------------
| 1 | 123| 23/08/15 | 15/10/2015| 12710 | 10613 |
| 2 | 123| 23/09/15 | NULL | 12710 | NULL |
| 3 | 123| 23/10/15 | NULL | 2096 | NULL |
| 4 | 123| 23/11/15 | NULL | 917 | NULL |
| 5 | 123| 23/12/15 | NULL | 917 | NULL |
| 6 | 123| 23/01/16 | 03/03/2016| 83913 | 78416 |
| 7 | 123| 23/02/16 | 19/04/2016| 267688| 168507|
| 8 | 123| 23/03/16 | 24/05/2016| 217168| 10891 |
| 9 | 123| 25/04/16 | NULL | 48661 | NULL |
| 10 | 123| 25/05/16 | 04/07/2016| 67824 | 47921 |
| 11 | 123| 23/06/16 | 07/08/2016|111612 | 35782 |
| 12 | 123| 25/07/16 | NULL | 63691 | NULL |
| 13 | 123| 25/08/16 | NULL | 30688 | NULL |
| 14 | 123| 25/09/16 | NULL | 30688 | NULL |
| 15 | 224| 26/05/16 | 25/07/2016| 41325 | 33075 |
| 16 | 224| 20/06/16 | 29/08/2016| 41325 | 5875 |
| 17 | 224| 20/07/16 | NULL | 41325 | NULL |
| 18 | 224| 18/08/16 | NULL | 8250 | NULL |
| 19 | 224| 20/09/16 | NULL | 2375 | NULL |
--------------------------------------------------------
我必须设置的条件是:
CASE WHEN SubDate > RecDate THEN (SubAmt + RecAmt)
ELSE SubAmt
END AS Cumulative
示例1: 从Desired Output可以看出,
对于ID 1:23/08/15(ID 1的子日期)< 2015年10月15日(ID 1的RecDate)。因此,ID 1的累积= 12710(ID 1的SubAmt)。
示例2: 对于ID 3:23/10/15(ID 3的子日期)> 2015年10月15日(ID 1的记录)。因此,ID 3的累积= 2096(ID 3的SubAmt)+ 10613(ID 1的RecAmt)= 12710。
示例3: 对于ID 8,23 / 03/16(ID 8的子日期)> ID 1和1的RecDate 6 2015年10月15日(ID 1的记录) 03/03/2016(ID 6的RecDate)
因此,ID 8的累积数:217168(ID 3的SubAmt)+ 10613(ID 1)+ 78416(ID 6)= 306197
期望的输出:
| ID | Class | SubDate | RecDate | SubAmt| RecAmt| Cumulative
-------------------------------------------------------------------
| 1 | 123| 23/08/15 | 15/10/2015| 12710 | 10613 | 12710
| 2 | 123| 23/09/15 | NULL | 12710 | NULL | 12710
| 3 | 123| 23/10/15 | NULL | 2096 | NULL | 12710
| 4 | 123| 23/11/15 | NULL | 917 | NULL | 11530
| 5 | 123| 23/12/15 | NULL | 917 | NULL | 11530
| 6 | 123| 23/01/16 | 03/03/2016| 83913 | 78416 | 94527
| 7 | 123| 23/02/16 | 19/04/2016| 267688| 168507| 278302
| 8 | 123| 23/03/16 | 24/05/2016| 217168| 10891 | 306197
| 9 | 123| 25/04/16 | NULL | 48661 | NULL | 306197
| 10 | 123| 25/05/16 | 04/07/2016| 67824 | 47921 | 336252
| 11 | 123| 23/06/16 | 07/08/2016|111612 | 35782 | 380040
| 12 | 123| 25/07/16 | NULL | 63691 | NULL | 380040
| 13 | 123| 25/08/16 | NULL | 30688 | NULL | 382820
| 14 | 123| 25/09/16 | NULL | 30688 | NULL | 382820
| 15 | 224| 26/05/16 | 25/07/2016| 41325 | 33075 | 41325
| 16 | 224| 20/06/16 | 29/08/2016| 41325 | 5875 | 41325
| 17 | 224| 20/07/16 | NULL | 41325 | NULL | 41325
| 18 | 224| 18/08/16 | NULL | 8250 | NULL | 41325
| 19 | 224| 20/09/16 | NULL | 2375 | NULL | 41325
--------------------------------------------------------------
请劝告。
我试过这个:
SELECT [ID]
,[Class]
,[SubDate]
,[RecDate]
,[SubAmt]
,[RecAmt]
,SUM (CASE
WHEN SubDate > RecDate or RecDate IS NULL THEN (SubAmt + RecAmt)
ELSE SubAmt END ) OVER (PARTITION BY [Class] ORDER BY [ID] ROWS
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Cumulative
FROM TableName
但它不起作用,而是给我一个不符合要求的输出。
输出错误:
| ID | Class | SubDate | RecDate | SubAmt| RecAmt| Cumulative (Wrong Output)
--------------------------------------------------------------------------
| 1 | 123| 23/08/15 | 15/10/2015| 12710 | 10613 | 12710
| 2 | 123| 23/09/15 | NULL | 12710 | NULL | 12710
| 3 | 123| 23/10/15 | NULL | 2096 | NULL | 12710
| 4 | 123| 23/11/15 | NULL | 917 | NULL | 12710
| 5 | 123| 23/12/15 | NULL | 917 | NULL | 12710
| 6 | 123| 23/01/16 | 03/03/2016| 83913 | 78416 | 96623
| 7 | 123| 23/02/16 | 19/04/2016| 267688| 168507| 364312
| 8 | 123| 23/03/16 | 24/05/2016| 217168| 10891 | 581480
| 9 | 123| 25/04/16 | NULL | 48661 | NULL | 581480
| 10 | 123| 25/05/16 | 04/07/2016| 67824 | 47921 | 649304
| 11 | 123| 23/06/16 | 07/08/2016|111612 | 35782 | 760917
| 12 | 123| 25/07/16 | NULL | 63691 | NULL | 760917
| 13 | 123| 25/08/16 | NULL | 30688 | NULL | 760917
| 14 | 123| 25/09/16 | NULL | 30688 | NULL | 760917
| 15 | 224| 26/05/16 | 25/07/2016| 41325 | 33075 | 41325
| 16 | 224| 20/06/16 | 29/08/2016| 41325 | 5875 | 82650
| 17 | 224| 20/07/16 | NULL | 41325 | NULL | 82650
| 18 | 224| 18/08/16 | NULL | 8250 | NULL | 82650
| 19 | 224| 20/09/16 | NULL | 2375 | NULL | 82650
--------------------------------------------------------------
请帮助我建议我应该如何更改SQL代码以及我应该如何适应case语句。谢谢大家!
答案 0 :(得分:1)
当RecDate
为NULL
时,SubDate > RecDate
条件将失败,因为NULL
未知且无法与任何值进行比较。
尝试这样的事情
SUM(CASE WHEN SubDate > RecDate or RecDate IS NULL THEN (SubAmt + RecAmt)
ELSE SubAmt
END AS Cumulative) OVER(PARTITION BY [Class] ORDER BY [ID] ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW)