How to extract unique days between two timestamps in BigQuery?

后端 未结 2 765
北荒
北荒 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.

提交回复
热议问题