How to extract unique days between two timestamps in BigQuery?

后端 未结 2 740
北荒
北荒 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: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;
    

提交回复
热议问题