查询一年内的日期

时间:2010-03-22 20:27:32

标签: sql postgresql

我的表在Postgres上是这样的,请注意,所有日子都从01开始,每月只有1个条目+年

SELECT * FROM "fis_historico_receita" 
+----+------------+---------------+
| id | data       | receita_bruta |
+----+------------+---------------+
| 1  | 2010-02-01 | 100000.0      |
| 2  | 2010-01-01 | 100000.0      |
| 3  | 2009-12-01 | 100000.0      |
| 4  | 2009-11-01 | 100000.0      |
| 5  | 2009-10-01 | 100000.0      |
| 6  | 2009-09-01 | 100000.0      |
| 7  | 2009-08-01 | 100000.0      |
| 8  | 2009-07-01 | 100000.0      |
| 9  | 2009-06-01 | 100000.0      |
| 10 | 2009-05-01 | 100000.0      |
| 11 | 2009-04-01 | 100000.0      |
| 12 | 2009-03-01 | 100000.0      |
| 13 | 2009-02-01 | 100000.0      |
| 14 | 2009-01-01 | 100000.0      |
| 15 | 2008-12-01 | 100000.0      |
+----+------------+---------------+

我想要的是找到从当前开始的12个月。

我试过了:

select *
from fis_historico_receita
where data in interval '1 year'

我真的想要使用Interval 的答案,+1适用于在Postgres上运行的所有人

1 个答案:

答案 0 :(得分:1)

试试这个:

select *
from fis_historico_receita
where data BETWEEN NOW() - interval '1 year' AND NOW()