0

我想返回一个结果集(来自 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 条件: where ... and last_payment > 100 days

4

2 回答 2

0

您可以having在最后添加:

having trunc('days', now()-max(paymentdate)) > 100
于 2012-06-05T08:32:52.250 回答
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;
于 2012-06-05T08:33:09.287 回答