BigQuery: how to group and count rows within rolling timestamp window?

前端 未结 1 1565
臣服心动
臣服心动 2020-12-16 08:47

I have some experience with MongoDB and I\'m learning about BigQuery. I\'m trying to perform the following task, and I don\'t know how to do it using BigQuery\'s standard SQ

相关标签:
1条回答
  • 2020-12-16 09:09

    Below is for BigQuery Standard SQL (see Enabling Standard SQL

    I am using ts as a field name (instead timestamp as it is in your example) and assume this field is of TIMESTAMP data type

    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
    )
    SELECT 
      url, event_id, day, events, 
      SUM(events) 
        OVER(PARTITION BY url, event_id ORDER BY sec 
          RANGE BETWEEN 259200 PRECEDING AND CURRENT ROW
      ) AS rolling3daysEvents
    FROM dailyAggregations
    -- ORDER BY url, event_id, day
    

    The value of 259200 is actually 3x24x3600 so sets 3 days range, so you can set whatever actual rolling period you need

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