I know this is a simple question, but I just started to explore Redshift and couldn't google the answer yet.
In MSSQL server I use the following WHERE CLAUSE:
Last quarter:
WHERE DateTime>= DATEADD(qq,DATEDIFF(qq,0,GETDATE())-1,0) AND DateTime < DATEADD(qq,DATEDIFF(qq,0,GETDATE())-0,0)
Last 2 months
WHERE DateTime>= DATEADD(mm,DATEDIFF(mm,0,GETDATE())-2,0) AND DateTime < DATEADD(mm,DATEDIFF(mm,0,GETDATE())-0,0)
In the Redshift for the quarter I came out to:
WHERE DateTime>= dateadd(qtr,datediff(qtr,'1970-01-01',current_date)-1,'1970-01-01') AND
DateTime < dateadd(qtr,datediff(qtr,'1970-01-01',current_date)-0,'1970-01-01')
For the last 2 months it looks like:
WHERE DateTime>= dateadd(month,datediff(month,'1970-01-01',current_date)-2,'1970-01-01') AND
DateTime < dateadd(month,datediff(month,'1970-01-01',current_date)-0,'1970-01-01')
So, I have to use the unix epoch value instead of 0. Is there any better way to set this up?
Thank you very much!
In Redshift, I would use date_trunc()
and interval arithmetics.
Last quarter:
where datetime >= date_trunc('quarter', current_date) - interval '1 quarter'
and datetime < date_trunc('quarter', current_date)
Last two months:
where datetime >= date_trunc('month', current_date) - interval '2 month'
and datetime < date_trunc('month', current_date)