How to extract unique days between two timestamps in BigQuery?

后端 未结 2 741
北荒
北荒 2021-01-14 05:49

for two different timestamps, let\'s say timestamp(\'2015-02-01\') and timestamp (\'2015-02-12\'), I want a column with all the dates in between. Like this (12 Rows) 2015-

相关标签:
2条回答
  • 2021-01-14 06:30

    You can do that with a cross join on a public dataset (fh-bigquery:geocode.numbers_65536) where you have numbers up to: 65536

    SELECT date(DATE_ADD(DAY, i, "DAY")) DAY
    FROM
      (SELECT '2015-01-01' AS DAY) a CROSS
    JOIN
      (SELECT i
       FROM [fh-bigquery:geocode.numbers_65536]
       WHERE i<=abs(DATEDIFF('2015-01-01','2015-01-15'))) b
    ORDER BY DAY ASC
    

    this outputs:

    +-----+------------+---+
    | Row |    day     |   |
    +-----+------------+---+
    |   1 | 2015-01-01 |   |
    |   2 | 2015-01-02 |   |
    |   3 | 2015-01-03 |   |
    |   4 | 2015-01-04 |   |
    |   5 | 2015-01-05 |   |
    |   6 | 2015-01-06 |   |
    |   7 | 2015-01-07 |   |
    |   8 | 2015-01-08 |   |
    |   9 | 2015-01-09 |   |
    |  10 | 2015-01-10 |   |
    |  11 | 2015-01-11 |   |
    |  12 | 2015-01-12 |   |
    |  13 | 2015-01-13 |   |
    |  14 | 2015-01-14 |   |
    |  15 | 2015-01-15 |   |
    +-----+------------+---+
    

    You can add this data to your view in the BigQuery UI by adding the project fh-bigquery using the project menu (the drop-down next to the project name, Switch to Project ➪ Display Project). Alternately, you can navigate to the BigQuery UI link https://bigquery.cloud.google.com/project/fh-bigquery After you add the project, the sample dataset (fh-bigquery) appears in the navigation panel.

    0 讨论(0)
  • 2021-01-14 06:36

    @Pentium10 answer is the right and classic way of filling ranges. Just for fun, I wanted to also give an alternative which doesn't rely on any additional table. This solution goes like that:

    1. Use RPAD to generate string of required length, i.e. number of days in the interval
    2. Use SPLIT to convert it to repeated field with number of elements equal to the number of days in the interval
    3. Use POSITION to get sequential index of each element in repeated field

    Here is the whole query put together:

    select date(date_add(day, i, "DAY")) day
    from  (select '2015-01-01' day) a 
    cross join
    (select 
       position(
         split(
           rpad('', datediff('2015-01-15','2015-01-01')*2, 'a,'))) i 
     from (select NULL)) b;
    
    0 讨论(0)
提交回复
热议问题