我对PostgreSQL中的extract
函数有疑问。
我正在生成有关上个月数据的报告。
到目前为止没有问题:
where extract (month from date) = extract(month from current_date- '1 month'::interval)
但这种设置的问题是,当数据超过1年时,将包括更多月份。
为了解决这个问题,我可以为年添加另一个条件:
and extract (year from date) = extract (year from current_date)
但是这会在1月份生成关于12月的报告时引起问题。
如何在1月份生成关于12月份的报告,而不必担心我会包含更多月份。
答案 0 :(得分:2)
这个怎么样:
WHERE date_trunc('month',date)=date_trunc('month',current_date) - interval '1 month'