在30天内显示重复的客户

时间:2015-03-19 09:26:15

标签: postgresql

我想在30天(V_Date)期间显示重复3个或更多同一客户的结果

所以,表格看起来像

     Name      V_Date  
    Arun Kumar 2015-30-01  
    Arun Kumar 2015-31-01  
    Arun Kumar 2015-12-02  
      Vijay    2015-01-01  
      Vijay    2015-28-01  
      Vijay    2015-15-02  
      vinoth   2015-02-01  
      vinoth   2015-02-06  
      vinoth   2015-02-08  
      Anand    2015-01-01  
      Anand    2015-02-02  
      Anand    2015-03-03  

预期产出

     Name        V_Date  
    Arun Kumar 2015-30-01  
    Arun Kumar 2015-31-01  
    Arun Kumar 2015-12-02  
      vinoth   2015-02-01  
      vinoth   2015-02-06  
      vinoth   2015-02-08  

目前能够使用HAVING COUNT(名称)> = 3选择重复的3个或更多客户,但在30天内无法检查V_Date。如何获得预期产出?......基于以下条件: -

 Arun Kumar (V_Date 31-01-2015 to 12-02-2015) 
             visit (3 or more time) within 30 days

  Vijay      (V_Date 01-01-2015 to 15-02-2015) 
             Not visit (3 or more time) within 30 days

  Vinoth     (V_Date 01-02-2015 to 08-02-2015) 
             visit (3 or more time) within 30 days

  Anand      (V_Date 01-01-2015 to 03-03-2015) 
             Not visit (3 or more time) within 30 days

1 个答案:

答案 0 :(得分:0)

只需添加where语句:

....  WHERE (w.deliverydate BETWEEN (NOW() - interval '1 months')::date AND NOW()::date)

然后使用Having语句 如果这不是你想要的,请更具体