【问题标题】:Number of active sku's by days活跃 sku 的天数
【发布时间】:2021-06-26 14:47:55
【问题描述】:

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

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

并期望有两种变体的活跃产品(不包括大括号中的 cmets):

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

【问题讨论】:

    标签: postgresql google-bigquery window-functions


    【解决方案1】:

    以下是 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
    

    【讨论】:

    • 很高兴它对你有用。也考虑投票(如果还没有):o)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多