在bigquery中运行多个条件的总数

问题描述 投票:1回答:2

我需要计算运行总额,但需要根据条件重设总计(当预期达到= 0且product_group和产品更改时)。在没有两个额外字段的情况下获得了帮助:Calculate a running total with a condition in BigQuery我有此表,并且可以将product_group和product用作整数或字符串,如下所示。

Date, Product_group, Product, Registrations, Expected Registrations, Expected Reached, Running Total
            2020-03-01,A, Bikes, 5, 4,1, 1
            2020-03-02,A, Bikes, 7, 5,1, 2
            2020-03-03,A, Bikes, 8, 6,1, 3
            2020-03-04,A, Bikes, 2, 5,0, 0
            2020-03-05,A, Bikes, 5, 4,1, 1
            2020-03-06,A, Bikes, 7, 5,1, 2 
            2020-03-04,B, Cars , 2, 5,0, 0
            2020-03-05,B, Cars , 5, 4,1, 1
            2020-03-06,B, Cars , 7, 5,1, 2
            2020-03-07,B, Cars , 8, 6,1, 3 
            2020-03-08,C, Plane, 2, 5,0, 0

关于如何修改此查询(其他帖子的答案)的任何建议,都可以在没有两个额外字段的情况下正常工作-

#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`
)

问题是当product_group或产品更改时COUNTIF(Expected_reached = 0) OVER(ORDER BY date ) grp重新开始,并且我得到了非唯一的组,因此运行的总计SUM(Expected_reached) OVER(PARTITION BY grp ORDER BY date ) Running_Total无法正确计算。

sql google-cloud-platform google-bigquery window-functions gaps-and-islands
2个回答
1
投票

您只需要将PARTITION BY Product_group, Product添加到两个分析函数中

#standardSQL
SELECT * EXCEPT(grp), 
  SUM(Expected_reached) OVER(PARTITION BY Product_group, Product, grp ORDER BY `date`) Running_Total
FROM (
  SELECT *, COUNTIF(Expected_reached = 0) OVER(PARTITION BY Product_group, Product ORDER BY `date`) grp 
  FROM `project.dataset.table`
)

0
投票

就您而言,您只需要在窗口函数的partition子句中添加product_groupproduct这两个附加列:

select 
    * except(grp), 
    sum(expected_reached) 
        over(partition by grp, product_group, product order by `date`) running_total
from (
    select 
        *, 
        countif(expected_reached = 0) 
            over(partition by product_group, product order by `date`) grp 
    from `project.dataset.table` 
)
© www.soinside.com 2019 - 2024. All rights reserved.