我想让我的查询在日期方面更加灵活,我正在考虑定义参照当前日期的变量。我正在考虑定义参照当前日期的变量。目前我正在为上一季度、前一季度和一年前的季度编码准确的日期。
这就是Q1的查询。
select '2020_Q1' as time_frame, id, status, date, agent, country, sale
from sales
where date >= '2020-01-01' and date < '2020-03-31'
我对第4季度和第1季度(2019年)也做同样的查询,最后再进行联合。今天是4月27日,比如说4个月后是8月27日。现在,我想看看Q2,Q1和Q2(2019)。我相信,我需要使用current_date,但如果你认为有更有效的方法,请告诉我。
你可以使用 date_trunc()
以获得 "当前季度 "的开始。
select ...
from sales
where date => date_trunc('quarter', current_date)
and date < date_trunc('quarter', current_date) + interval '3 months'