对不起,不好的话题-我需要计算一个总计,但需要在某个条件下重置总计(当预期达到= 0时)。我有这张桌子:
Date, Registrations, Expected Registrations, Expected reached 2020-03-01, 5, 4,1 2020-03-02, 7, 5,1 2020-03-03, 8, 6,1 2020-03-04, 2, 5,0 2020-03-05, 5, 4,1 2020-03-06, 7, 5,1 2020-03-07, 8, 6,1 2020-03-08, 2, 5,0
运行总计的预期结果-条件是应计算“预期达到” <0的运行总计。如果``预期到达’‘= 0,则运行总数应从0开始:
Date, Registrations, Expected Registrations, Expected Reached, Running Total 2020-03-01, 5, 4,1, 1 2020-03-02, 7, 5,1, 2 2020-03-03, 8, 6,1, 3 2020-03-04, 2, 5,0, 0 2020-03-05, 5, 4,1, 1 2020-03-06, 7, 5,1, 2 2020-03-07, 8, 6,1, 3 2020-03-08, 2, 5,0, 0
我不知道如何对我的窗口函数进行分区以实现此目的。也许我必须在之前创建中间计算,但不确定。有什么建议?
编辑:edit2:删除了我的“即时问题”。
以下是BigQuery标准SQL
#standardSQL SELECT * EXCEPT(grp), SUM(Expected_reached) OVER(PARTITION BY grp ORDER BY `date`) Running_Total FROM ( SELECT *, COUNTIF(Expected_reached = 0) OVER(ORDER BY `date`) grp FROM `project.dataset.table` )