0

我想计算没有。状态设置为 1 的天数。每次状态为 0 时,计数应从 1 重新开始。

注意:不必是连续天数

输入

UserId | Day        | Status|
A      |2021-09-16  | 1     |
A      |2021-09-17  | 1     |
A      |2021-09-18  | 1     |
A      |2021-09-19  | 1     |
A      |2021-09-20  | 0     |
A      |2021-09-21  | 1     |
B      |2021-09-16  | 1     |
B      |2021-09-17  | 1     |
B      |2021-09-20  | 1     |
B      |2021-09-21  | 0     |

预期产出

UserId | Day        | Status| Cum_Sum |
A      |2021-09-16  | 1     | 1       |
A      |2021-09-17  | 1     | 2       |
A      |2021-09-18  | 1     | 3       |
A      |2021-09-19  | 1     | 4       |
A      |2021-09-20  | 0     | 0       |
A      |2021-09-21  | 1     | 1       |
B      |2021-09-16  | 1     | 1       |
B      |2021-09-17  | 1     | 2       |
B      |2021-09-20  | 1     | 3       |
B      |2021-09-21  | 0     | 0       |
4

1 回答 1

1

您可以使用以下row_cumsum()功能

datatable(UserId:string, Day:datetime, Status:int)
[
    "A", datetime(2021-09-16), 1,
    "A", datetime(2021-09-17), 1,
    "A", datetime(2021-09-18), 1,
    "A", datetime(2021-09-19), 1,
    "A", datetime(2021-09-20), 0,
    "A", datetime(2021-09-21), 1,
    "B", datetime(2021-09-16), 1,
    "B", datetime(2021-09-17), 1,
    "B", datetime(2021-09-20), 1,
    "B", datetime(2021-09-21), 0,
]
| order by UserId asc, Day asc
| extend cumulative_sum = row_cumsum(Status, prev(UserId) != UserId or Status == 0)
于 2021-09-29T23:20:30.990 回答