现在和最大日期值之间的日期差异大于X.

时间:2012-06-05 08:28:45

标签: sql postgresql date max datediff

我想返回一个结果集(来自postgres数据库),其中包含上次付款日期超过X天前的客户。

这是我目前要获取所有客户的列表及其上次付款日期:

  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 ...和last_payment> 100天

2 个答案:

答案 0 :(得分:0)

您最后可以添加having

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

答案 1 :(得分: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;