Redshift does not have a generate_series() function for the date range, but you can generate a series using the following steps ...
Step 1: Create a genid table and insert a constant value as 1 for the number of times you need to create the series. If you need a series to be created within 12 months, you can insert 12 times. You can better insert a few more times, for example, 100, so that you do not encounter any problem.
create table genid(id int)
------------ for the number of months of insertion into the values ββof geniuses (1)
Step 2: The table for which you need to create a 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 a 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
source share