Where子句= postgreSQL中周日结束的一周

时间:2016-12-22 20:25:44

标签: sql postgresql

在我的where子句中,我必须手动更改两个子句的周数(“Week”和“Can_Week_End”)。在我的例子中,周末在星期日结束(12 / 4,12 / 11,12 / 18,等等......)。如何操作where子句过滤器以使日期自动更改。如果有人查询

select * from myview
周一早上,它将显示通过截止日期(前一天)的最后一周的数据。有没有办法实现这个目标?请提供见解。

create view myview 
as 
select case when sd."Build_Spec" = 'Spec' then 'X' else '' end as "Spec", 
       case when sd."Build_Spec" = 'Build' then 'X' else '' end as "Build", 
       Case when sd."Can_Week_End" = '' then '' else 'X' end as "Cancel",
sd."Price", 
sd."Sq_Ft" as "SF", 
sd."Price"::money / to_number(sd."Sq_Ft",'99999999') as "PPSF", 
case when sd."Realtor_Sale" is not null then 'X' else '' end as "Realtor"
from datapcr sd 
left join comppcr ad on "Address" =  "Address_of_New_Home"
where sd."Week" = '2016-12-18'or sd."Can_Week_End" = '12/18/2016'
order by "Cancel"

1 个答案:

答案 0 :(得分:0)

目前还不清楚为什么在同一个查询中有两种不同的日期格式,而且还不清楚你想要在哪里准确地绘制线条。但基本上,您可以使用date_trunc()解决此问题:

...
where sd."Week" = date_trunc('week', now())::date - 1
...

date_trunc('week', now())返回当周的00:00。转换为date并减去1以获取上周日的日期。瞧。

相关: