Sql Postgres-如何显示上一年的数据?

Sql Postgres-如何显示上一年的数据?,sql,postgresql,Sql,Postgresql,我有sql select name, tanggal, status from tbl_person where status = 'PROSES' and date_part('year', tanggal) = 2021 - INTERVAL '1 YEAR' tanggal的日期为2021-01-01 我想显示上一年的数据,例如2020年,如何编写正确的查询?使用您的方法: where status = 'PROSES' and

我有sql

select
    name,
    tanggal,
    status
from
    tbl_person
where
    status = 'PROSES'
    and date_part('year', tanggal) = 2021 - INTERVAL '1 YEAR'
tanggal的日期为2021-01-01

我想显示上一年的数据,例如2020年,如何编写正确的查询?

使用您的方法:

where status = 'PROSES' and
      date_trunc('year', tanggal) = date_trunc('year', current_date) - interval '1 year'
但是,我更喜欢避免使用列上的函数——因此查询更容易优化。因此,我建议:

where status = 'PROSES' and
      tanggal < date_trunc('year', now()) and
      tanggal >= date_trunc('year', now()) - interval '1 year'
其中status='PROSES'和
tanggal=日期(“年”,现在())-间隔“1年”
这只是使用
now()
而不是
current\u date
,因为它更容易键入