简体   繁体   English

如何在Amazon Redshift查询select上个季度/最近2个月的数据

[英]How to select last quarter / last 2 months of data in Amazon Redshift query

I know this is a simple question, but I just started to explore Redshift and couldn't google the answer yet.我知道这是一个简单的问题,但我刚刚开始探索 Redshift,还无法用谷歌搜索答案。

In MSSQL server I use the following WHERE CLAUSE:在 MSSQL 服务器中,我使用以下 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最近 2 个月

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:在本季度的 Redshift 中,我得出以下结论:

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:在过去的 2 个月里,它看起来像:

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?所以,我必须使用 unix 纪元值而不是 0。有没有更好的方法来设置它?

Thank you very much!非常感谢你!

In Redshift, I would use date_trunc() and interval arithmetics.在 Redshift 中,我会使用date_trunc()和区间算法。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM