运行 总共有 bigquery 中的几个条件
Running total with several conditions in bigquery
我需要计算 运行 总计,但需要根据条件重置总计(当预期达到 = 0 且 product_group 并且产品发生变化时)。在没有两个额外字段的情况下在此处获得帮助:
我有这个 table 并且可以使用 product_group 和乘积作为整数或字符串,如下所示。
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
有关如何调整此查询(来自另一个 post 的答案)的任何建议,该查询在没有两个额外字段的情况下也能正常工作-
#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`
)
问题是 COUNTIF(Expected_reached = 0) OVER(ORDER BY
日期) grp
在 product_group 或产品更改时重新开始,我得到非唯一组所以 运行 总计 SUM(Expected_reached) OVER(PARTITION BY grp ORDER BY
日期) Running_Total
计算不正确。
就此而言,您只需将 product_group
和 product
这两个附加列添加到 window 函数的分区子句中:
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`
)
您只需要将 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 且 product_group 并且产品发生变化时)。在没有两个额外字段的情况下在此处获得帮助:
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
有关如何调整此查询(来自另一个 post 的答案)的任何建议,该查询在没有两个额外字段的情况下也能正常工作-
#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`
)
问题是 COUNTIF(Expected_reached = 0) OVER(ORDER BY
日期) grp
在 product_group 或产品更改时重新开始,我得到非唯一组所以 运行 总计 SUM(Expected_reached) OVER(PARTITION BY grp ORDER BY
日期) Running_Total
计算不正确。
就此而言,您只需将 product_group
和 product
这两个附加列添加到 window 函数的分区子句中:
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`
)
您只需要将 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`
)