我有一张下表projects
。
id title created_at claim_window
1 Project One 2012-05-08 13:50:09.924437 5
2 Project Two 2012-06-01 13:50:09.924437 10
A)我想通过计算deadline = created_at + claim_window(No. of days)
找到截止日期。
如下所示。
id title created_at claim_window deadline
1 Project One 2012-05-08 13:50:09.924437 5 2012-05-13 13:50:09.924437
2 Project Two 2012-06-01 13:50:09.924437 10 2012-06-11 13:50:09.924437
B]我也想找到截止日期已经消失的项目
id title created_at claim_window deadline
1 Project One 2012-05-08 13:50:09.924437 5 2012-05-13 13:50:09.924437
我尝试了类似的事情。
SELECT * FROM "projects" WHERE (DATE_PART('day', now()- created_at) >= (claim_window+1))
但由于某种原因它无效。
答案 0 :(得分:90)
这将为您提供截止日期:
select id,
title,
created_at + interval '1' day * claim_window as deadline
from projects
要获得截止日期结束的所有项目,请使用:
select *
from (
select id,
created_at + interval '1' day * claim_window as deadline
from projects
) t
where localtimestamp at time zone 'UTC' > deadline
答案 1 :(得分:8)
对我来说,我必须将整个区间用单引号而不仅仅是区间的值。
select id,
title,
created_at + interval '1 day' * claim_window as deadline from projects
而不是
select id,
title,
created_at + interval '1' day * claim_window as deadline from projects