generate_series function in Amazon Redshift

风格不统一 提交于 2019-11-29 00:07:18

The version of generate_series() that supports dates and timestamps was added in Postgres 8.4.

As Redshift is based on Postgres 8.0, you need to use a different way:

select timestamp '2011-12-31 00:00:00' + (i * interval '1 day')
from  generate_series(1, (date '2012-12-31' - date '2011-12-31')) i;

If you "only" need dates, this can be abbreviated to:

select date '2011-12-31' + i
from  generate_series(1, (date '2012-12-31' - date '2011-12-31')) i;

generate_series is working with Redshift now.

SELECT CURRENT_DATE::TIMESTAMP  - (i * interval '1 day') as date_datetime 
FROM generate_series(1,31) i 
ORDER BY 1

This will generate last 30 days date

Partha Sarathi Sarkar

There is no generate_series() function in Redshift for Date Range but you can generate the series with below steps...

Step 1: Created a table genid and insert constant value as 1 for number of times you need to generate the series. If you need the series to be generated for 12 month you can insert 12 times. Better you can insert for more number of times like 100, so that you do not face any issue.

create table genid(id int)

------------ for number of months insert into genid values(1)

Step 2: The table for which you need to generate the series.

create table pat(patid varchar(10),stdt timestamp, enddt timestamp);

insert into pat values('Pat01','2018-03-30 00:00:00.0','2018-04-30 00:00:00.0')

insert into pat values('Pat02','2018-02-28 00:00:00.0','2018-04-30 00:00:00.0')

insert into pat values('Pat03','2017-10-28 00:00:00.0','2018-04-30 00:00:00.0')

Step 3: This query will generate the series for you.

with cte as 
(
select max(enddt) as maxdt
from pat
) ,
cte2 as(
select dateadd('month', -1 * row_number() over(order by 1),  maxdt::date ) as gendt  
from  genid , cte
) select * 
from pat, cte2
where gendt between stdt and enddt
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!