【问题标题】:How to fill missing dates in BigQuery by partition (no backfill)如何按分区填充 BigQuery 中缺失的日期(无回填)
【发布时间】:2021-09-17 03:18:03
【问题描述】:

How to fill missing dates in BigQuery?相关,但没有后向填充/前向填充:

假设我有以下销售数据:

order_date      product         store           sales
_______________________________________________________
2021-05-01      1               1               10
2021-05-04      1               1               5

2021-04-25      1               2               3
2021-04-30      1               2               9
2021-05-10      1               2               1

2021-05-02      2               1               3
2021-05-04      2               1               3

我想要的输出是用 0 填充空日期,但在 productstore 上进行分区(而不是回填到全局 MIN(DATE) 或前向填充到全局 MAX(DATE)

order_date      product         store           sales
_______________________________________________________
2021-05-01      1               1               10
2021-05-02      1               1               0
2021-05-03      1               1               0
2021-05-04      1               1               5

2021-04-25      1               2               3
2021-04-26      1               2               0
...             ...             ...             ... (all 0s)
2021-04-29      1               2               0
2021-04-30      1               2               9
...             ...             ...             ... (all 0s)
2021-05-10      1               2               1

2021-05-02      2               1               3
2021-05-03      2               1               0
2021-05-04      2               1               3

我将如何实现这一点(特别是在 BigQuery 中)?

我最初的想法是我可能需要创建一个类似的表

SELECT 
    product, store,
    MIN(order_date) min_date,
    MAX(order_date) max_date
FROM mydata
GROUP BY product, store

但我不确定如何使用它来对原始 mydata 表进行分区并用 IFNULL(sales,0) sales 填充日期之间的日期

提前致谢!

【问题讨论】:

  • 不确定问题是什么!您可能想提供更多详细信息
  • 所需的输出是一个类似的表,但对于任何特定的store/product 对,用 0 填充空日期以表示 sales

标签: sql google-bigquery


【解决方案1】:

考虑以下方法

select order_date, product, store, ifnull(sales, 0) sales 
from (
  select product, store, order_date 
  from (
    select product, store, min(order_date) start_date, max(order_date) end_date
    from `project.dataset.table`
    group by product, store
  ), unnest(generate_date_array(start_date, end_date)) order_date
)
left join `project.dataset.table` 
using(product, store, order_date)          

如果应用于您问题中的样本数据 - 输出是

【讨论】:

  • 这正是我所需要的,谢谢!
【解决方案2】:

使用cross join 生成所有行。然后使用left join 引入已有数据:

select order_date, sp.product, sp.store,
       coalesce(sales, 0) as sales
from (select distinct store, product from t) sp cross join
     unnest(generate_date_array(date '2021-04-01', date '2021-05-31', interval 1 day) day left join
     t
     on sp.store = t.store and sp.product = t.product and
        day = sp.order_date;

上面生成了 4 月和 5 月所有天的数据。您可以调整时间段。

【讨论】:

  • 交叉连接与 store/product 无关,并且会在与该对不相关的时间段内回填/转发填充 0。
猜你喜欢
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多