BigQuery: how to perform rolling timestamp window group count that produces row for each day

后端 未结 1 1099
时光说笑
时光说笑 2021-01-06 16:14

this is an extension to a question that I asked and resolved on StackOverflow here.

I\'m a BigQuery and SQL novice and I wanted to construct a Standard SQL query tha

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 17:06

    WITH dailyAggregations AS (
      SELECT 
        DATE(ts) AS day, 
        url, 
        event_id, 
        UNIX_SECONDS(TIMESTAMP(DATE(ts))) AS sec, 
        COUNT(1) AS events 
      FROM yourTable
      GROUP BY day, url, event_id, sec
    ),
    calendar AS (
      SELECT day
      FROM UNNEST (GENERATE_DATE_ARRAY('2016-08-28', '2016-11-06')) AS day
    )
    SELECT 
      c.day, url, event_id, events, 
      SUM(events) 
        OVER(PARTITION BY url, event_id ORDER BY sec 
          RANGE BETWEEN 259200 PRECEDING AND CURRENT ROW
      ) AS rolling4daysEvents
    FROM calendar AS c
    LEFT JOIN dailyAggregations AS a
    ON a.day = c.day
    

    0 讨论(0)
提交回复
热议问题