BigQuery StandardSQL: Last 7 Days using _TABLE_SUFFIX

前端 未结 3 1810
囚心锁ツ
囚心锁ツ 2020-12-17 16:32

Question: I want to pull data from multiple Google Analytics sessions tables using _TABLE_SUFFIX, but I want to set the suffix parameters to between \"seven

相关标签:
3条回答
  • 2020-12-17 17:08

    Elliott's answer is correct, but if you want to get the most performance out of BigQuery for such kind of query, instead of converting _TABLESUFFIX to DATE, you should convert CURRENT_DATE expressions to strings:

    WHERE
      _TABLE_SUFFIX BETWEEN 
      FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)) AND
      FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
    
    0 讨论(0)
  • 2020-12-17 17:22

    This works for anyone just looking to segment out the last week of data in big query. Works for any data set as long as you have a timestamp!

    where TIMESTAMPFIELD >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 7 DAY)
    
    0 讨论(0)
  • 2020-12-17 17:24

    You were almost there with your last attempt. You need to convert your string to a date in order to use it in the comparison:

    WHERE
      PARSE_DATE('%Y%m%d', _TABLE_SUFFIX) BETWEEN 
      DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AND
      DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
    
    0 讨论(0)
提交回复
热议问题