How to automatically update data from the 12 most recent weeks?

后端 未结 1 1295
失恋的感觉
失恋的感觉 2021-01-17 00:21

I would like to create two dynamic weekly BigQuery Firebase Reports, reflecting the most recent 12 Weeks of data for:

  1. Event Occurrences per W
相关标签:
1条回答
  • 2021-01-17 01:18

    It would be a lot faster if i did not have to manually input the date fields every week), but the script "knows" the week-number we are in, and updates the 12 most recent weeks' data given the week we are in.

    Below is for BigQuery Standard SQL

    WHERE _TABLE_SUFFIX 
      BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 12 * 7 + EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 2 DAY)) 
      AND FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 1 DAY))
    

    Below just shows the output

    #standardSQL
    SELECT 
      FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 2 * 7 + EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 2 DAY)) first_day,
      FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 1 DAY)) last_day  
    

    -

    first_day   last_day     
    20171002    20171015       
    

    Whenever you run above script - it will return you the start and end of most recent 12 weeks period

    Update for:

    I posted my first iteration answer above in what i would want

    #standardSQL
    SELECT 
      CONCAT(
        FORMAT_DATE('Week %W %d %B %Y, ', first_day),
        FORMAT_DATE('%d %B %Y, ', last_day), 
        FORMAT_DATE('%Y%m%d ', first_day),
        FORMAT_DATE('%Y%m%d', last_day)
      ) wk
    FROM (
      SELECT 
        DATE_SUB(CURRENT_DATE(), INTERVAL 1 * 7 + EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 2 DAY) first_day,
        DATE_SUB(CURRENT_DATE(), INTERVAL EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 1 DAY) last_day 
    )
    

    with the output

    wk   
    Week 41 09 October 2017, 15 October 2017, 20171002 20171015
    
    0 讨论(0)
提交回复
热议问题