活跃 SKU 天数

Number of active sku's by days

我有这个 table(包含有关发布开始和结束信息的产品 ):

SKU  start_time            end_time
id1   21.01.2020 14:10:00    22.01.2020 16:18:05
id1   23.01.2020 16:18:05    24.01.2020 19:03:14
id2   21.01.2020 16:18:05    21.01.2020 18:33:50
id3   25.01.2020 18:33:50    25.01.2020 19:03:14

并期望有两种变体(不包括大括号中的注释)按天计算的活跃产品:

date            active_sku    active sku_end_of_day
21.01.2020      2 (id1,id2)   1 (id1)
22.01.2020      1 (id1)       0
23.01.2020      1 (id1)       1 (id1)
24.01.2020      1 (id1)       0
25.01.2020      1 (id3)       0

以下适用于 BigQuery 标准 SQL

假设 start_timeend_time 是时间戳数据类型 - 请考虑以下

select date, 
  count(distinct SKU) as active_sku, 
  count(distinct if(offset = 0, null, SKU)) as active_sku_end_of_date
from `project.dataset.table`,
unnest(array_reverse(generate_date_array(date(start_time),date(end_time)))) date with offset
group by date
# order by date    

如果应用于您问题中的示例数据 - 输出为

如果 start_timeend_time 是字符串 - 您应该使用 parse_timestamp() 函数从字符串中解析时间戳 - 如下例

select date, 
  count(distinct SKU) as active_sku, 
  count(distinct if(offset = 0, null, SKU)) as active_sku_end_of_date
from `project.dataset.table`,
unnest(array_reverse(generate_date_array(date(parse_timestamp('%d.%m.%Y %T', start_time)),date(parse_timestamp('%d.%m.%Y %T', end_time))))) date with offset
group by date
# order by date