为每个品牌生成 date_array 的序列

Generate sequence of date_array for each brand

我知道如何使用以下内容在 Google BigQuery 中创建日期序列:

WITH  
  first_date AS (select min(first_listing_date) as day from `myexplorer-1575814757622.carsome.daily_listing`),
  last_date AS (select max(first_listing_date) as day from `myexplorer-1575814757622.carsome.daily_listing`)
select date_arr as my_date from UNNEST(GENERATE_DATE_ARRAY((select day from first_date), (select day from last_date))) as date_arr
;

my_date
----------
2020-08-11
2020-08-12
2020-08-13
2020-08-14
2020-08-15

假设我有品牌 A、B、C,并且我想为每个品牌生成上述日期序列。我被困在这里了。请帮忙。

car_brand
---------
Brand-A
Brand-B
Brand-C

简单的交叉连接就可以了。

例如

with dts 
as(
select date_arr as my_date 
from UNNEST(GENERATE_DATE_ARRAY('2022-01-01', '2022-01-05')) as date_arr)
, car_brands
as(
  select 'brand_a' 
  union all
  select 'brand_b' 
)
select *
from dts, car_brands

my_date f0_
01-01-2022  brand_a
01-01-2022  brand_b
02-01-2022  brand_a
02-01-2022  brand_b
03-01-2022  brand_a
03-01-2022  brand_b
04-01-2022  brand_a
04-01-2022  brand_b
05-01-2022  brand_a
05-01-2022  brand_b

试试这个:

WITH daily_listing AS (
SELECT DATE '2020-08-11' AS first_listing_date 
 UNION ALL
SELECT DATE '2020-08-15' AS first_listing_date 
),
dates AS (
  SELECT GENERATE_DATE_ARRAY(MIN(first_listing_date), MAX(first_listing_date)) my_dates
    FROM daily_listing
)
SELECT car_brand, my_date 
  FROM dates, UNNEST(my_dates) my_date, UNNEST(['Brand-A', 'Brand-B', 'Brand-C']) car_brand
 ORDER BY 1
;

输出: