PostgreSQL查询本周的数据

时间:2017-06-28 21:53:10

标签: sql postgresql

我正试图抓住本周最初尝试过的所有约会:

select * 
from   appointments 
where  staff_id = 'id' 
and   (to_timestamp(appointments.start_time)) > (current_date - interval '1 week') 
and   (to_timestamp(appointments.start_time)) < (current_date + interval '1 week') 

这只选择等于+/- 7天的所有约会,而不是当前周。这是我的下一次尝试。我知道它已经坏了,但我认为它得到了重点。

select * 
from   appointments 
where  staff_id = 'id' 
and    (to_timestamp(appointments.start_time)) > (current_date - interval concat(extract(dow from current_date), ' days')) 
and    (to_timestamp(appointments.start_time)) < (current_date + interval concat(7 - extract(dow from current_date), ' days'))

我如何构建此查询?

1 个答案:

答案 0 :(得分:0)

如果您同意Postgres对一周的定义,那么您可以这样做:

select a.*
from appointments a
where staff_id = 'id' and 
      to_timestamp(appointments.start_time) >= date_trunc('week', current_date) and
      to_timestamp(appointments.start_time) < date_trunc('week', current_date) + interval '1 week';