I am trying to create a running total column that resets each time it encounters a null or multiple null values. I expect to maintain the ID partition and date order. I expect the result as the table below
| ID | date | value | Running_T |
| ------------ | ------------ | ------------ | ------------ |
| SA223 | 10/07/2018 | 3 | 3 |
| SA223 | 11/07/2018 | 6 | 9 |
| SA223 | 25/07/2018 | NULL | NULL |
| SA223 | 16/08/2018 | 11 | 11 |
| SA223 | 17/08/2018 | 9 | 20 |
| SA555 | 15/05/2019 | 4 | 4 |
| SA555 | 27/05/2019 | 13 | 17 |
| SA555 | 11/07/2019 | NULL | NULL |
| SA555 | 21/11/2019 | NULL | NULL |
| SA555 | 20/02/2020 | 4 | 4 |
| SA555 | 27/05/2020 | 1 | 5 |
| SA555 | 23/07/2020 | 2 | 7 |
I tried the code below but it messed up the ordering and does not deal with Null values as I want.
SELECT ID
, date
, value
,Running_T = SUM(value) OVER(PARTITION BY ID, (CASE WHEN [value] IS NOT NULL THEN [value] ELSE NULL END) ORDER BY [date])
FROM dim.event_data
2条答案
按热度按时间ipakzgxi1#
This is a classic Gaps And Islands problem. There are more efficient solutions but a simple one is as follows:
You can use a conditional running
COUNT
to number each set of rows. Then do a runningSUM
over that group number to get the final resultdb<>fiddle
bkhjykvo2#
you can try like this