【问题标题】:How to get cumulative record per day in BigQuery如何在 BigQuery 中获取每天的累积记录
【发布时间】:2020-02-01 10:29:35
【问题描述】:

我有一个数据如下。

|-----------|-------------|---------------|
|order_date | customer_id | product_id    |
|-----------|-------------|---------------|
|2020-01-01 | 123456      | 0001          |
|-----------|-------------|---------------|
|2020-01-02 | 123456      | 0005          |
|-----------|-------------|---------------|
|2020-01-03 |123456       | 0010          |
|-----------|-------------|---------------|

然后我想像这样每天累积product_id。

|-----------|-------------|----------------------------|
|order_date |customer_id  |count_cumulative_product_id |
|-----------|-------------|----------------------------|
|2020-01-01 |123456       |1                           |
|-----------|-------------|----------------------------|
|2020-01-02 |123456       |2                           |
|-----------|-------------|----------------------------|
|2020-01-03 |123456       |3                           |         
|-----------|-------------|----------------------------|

我不知道什么样的查询可以解决这个问题......

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    以下是 BigQuery 标准 SQL

    #standardSQL
    SELECT *, 
      COUNT(1) OVER(PARTITION BY customer_id ORDER BY order_date) count_cumulative_product_id
    FROM `project.dataset.table`
    

    您可以使用您问题中的示例数据进行测试,使用上面的示例,如下例所示

    #standardSQL
    WITH `project.dataset.table` AS (
      SELECT '2020-01-01' order_date, 123456 customer_id, '0001' product_id UNION ALL
      SELECT '2020-01-02', 123456, '0005' UNION ALL
      SELECT '2020-01-03', 123456, '0010' 
    )
    SELECT *, 
      COUNT(1) OVER(PARTITION BY customer_id ORDER BY order_date) count_cumulative_product_id
    FROM `project.dataset.table`
    -- ORDER BY order_date   
    

    结果

    Row order_date  customer_id product_id  count_cumulative_product_id  
    1   2020-01-01  123456      0001        1    
    2   2020-01-02  123456      0005        2    
    3   2020-01-03  123456      0010        3
    

    【讨论】:

    • 非常感谢。这可以用于有很多行的表吗?
    • 当然。当然。分析功能非常有效
    • 成功了。非常感谢!
    【解决方案2】:

    如果您不担心distinct product_ids 的累积计数,那么您可以简单地使用“移动窗口”方法:

    select 
       order_date,
       customer_id,
       count(product_id) over (order by product_id range between unbounded preceding and current row) as cumulative_product_ids
    from `dataset.table`
    

    但是,如果你想要不同 product_ids 的累积计数,那么你可以使用类似的东西:

    select order_date, customer_id, count(distinct x) as cumulative_product_ids from ( 
       select 
          order_date, 
          customer_id, 
          array_agg(product_id) over (order by product_id range between unbounded preceding and current row) as cumulative_product_ids
    from `dataset.table`
    ), unnest(cumulative_product_ids) as x
    group by 1,2
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢。我已经尝试过了,我的数据集大约是 300MB,但是运行了 2 个多小时......所以我不得不放弃......
    • 300megs 应该不是问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2021-07-23
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多