2012-06-05 38 views
0

我想返回包含最近一次付款日期大于X天前的客户端的结果集(来自postgres数据库)。现在和最大日期值之间的日期差异大于X

这是我目前得到的所有客户名单和他们的最后付款日期:哪里......和last_payment>100天:

select usermaster.userid, date_trunc('days', now()-max(paymentdate)) as last_payment 
    from usermaster, paymentdetail 
    where usermaster.userid=paymentdetail.userid 
    group by usermaster.userid; 

我想和一个额外的where条件限制

回答

0

这应该做的伎俩

编辑 -

select * from 
(SELECT usermaster.userid, date_trunc('days', now()-max(paymentdate)) as 
     last_payment 
from usermaster, paymentdetail 
where usermaster.userid=paymentdetail.userid 
group by usermaster.userid) as temptab 
where last_payment>100; 
+0

感谢您的快速响应。我以前没遇到过HAVING选项。不幸的是它不起作用。 Postgres给出了错误:列“last_payment”不存在 – cemlo

+0

检查更新后的查询 –

+0

是的,这对我有效。我甚至能够将其修改为更新声明。谢谢! – cemlo

0

你可以在末尾添加having

having trunc('days', now()-max(paymentdate)) > 100