generate_series() method fails in Redshift

前端 未结 7 1438
时光说笑
时光说笑 2020-11-27 22:01

When I run the SQL Query:

 select generate_series(0,g)
 from ( select date(date1) - date(date2) as g from mytable ;

It returns an error:

相关标签:
7条回答
  • 2020-11-27 22:45

    You can use a window function to achieve a similar result. This requires an existing table (like stv_blocklist) to seed off that has at least the number of rows you need but not too many which might slow things down.

    with days as (
        select (dateadd(day, -row_number() over (order by true), sysdate::date)) as day 
        from [other_existing_table] limit 30
    )
    select day from days order by 1 asc
    

    You can use this method to get other time ranges as well for bucketing purposes. This version generates all the minutes for the previous day so you could do a left join against it and bucket your data.

    with buckets AS (
        select (dateadd(minute, -row_number() over (order by true), sysdate::date)) as minute 
        from [other_table] limit 1440
    )
    select minute from buckets order by 1 asc
    

    I may have first seen this here.

    0 讨论(0)
提交回复
热议问题