【问题标题】:SQL find Customers that haven't been contacted in the past 45 daysSQL 查找过去 45 天内未联系的客户
【发布时间】:2017-06-27 11:24:23
【问题描述】:

您能帮我筛选过去 xxxx 天内未联系过的客户吗? e.i 45

SELECT TOP 1000 [ID]
      ,[dates]
      ,[companies]
  FROM [company].[dbo].[Contact]

注意:有些公司在 xxxx 日被多次联系。

我认为获取最新日期会得到准确的结果。

ID  dates   companies

2017/01/02  Facebook
2017/01/03  Chevron
2017/01/05  AFLAC
2017/01/09  Applied Industrial Technologies
2017/01/11  Charter Communications
2017/01/12  Coca-Cola
2017/01/13  Dow Chemical
2017/01/17  Foot Locker
2017/01/18  General Dynamics
2017/01/20  Humana
2017/01/25  Imation
2017/01/26  Kimberly-Clark
2017/01/27  3M
2017/01/30  Facebook
2017/01/31  Chevron
2017/02/01  Foot Locker
2017/02/03  General Dynamics
2017/02/06  Humana
2017/02/10  Imation
2017/02/17  Kimberly-Clark
2017/02/20  3M
2017/02/21  Public Storage
2017/02/22  Qualcomm
2017/02/24  Sealy
2017/02/28  Steelcase
2017/02/02  Textron
2017/02/06  Men's Wearhouse
2017/02/08  Toll Brothers
2017/02/09  Tractor Supply
2017/02/13  USG
2017/02/14  Valspar
2017/02/15  Waste Management
2017/02/20  Williams-Sonoma
2017/02/21  Yahoo
2017/02/23  Union Pacific
2017/02/24  Foot Locker
2017/02/27  General Dynamics
2017/02/28  Humana
2017/03/01  Imation
2017/03/02  Kimberly-Clark
2017/03/03  3M
2017/03/06  Public Storage
2017/03/09  Qualcomm
2017/03/10  Sealy
2017/03/13  Steelcase
2017/03/14  Textron
2017/03/17  Men's Wearhouse
2017/03/23  Toll Brothers
2017/03/24  Tractor Supply
2017/03/27  USG
2017/03/01  Valspar
2017/03/03  Waste Management
2017/03/06  Williams-Sonoma
2017/03/07  Yahoo
2017/03/08  Union Pacific
2017/03/09  Foot Locker
2017/03/13  General Dynamics
2017/03/15  Humana
2017/03/22  Imation
2017/03/24  Kimberly-Clark
2017/03/27  3M
2017/03/28  Capital One Financial
2017/03/29  CBS
2017/04/03  Citigroup
2017/04/05  E*Trade Financial
2017/04/06  El Paso
2017/04/07  Goodrich
2017/04/10  Hanesbrands
2017/04/11  International Paper
2017/04/12  Eastman Kodak
2017/04/20  Kraft Foods
2017/04/25  LSI
2017/04/26  Macy's
2017/04/27  Brunswick
2017/04/28  Meredith
2017/04/03  Manpower
2017/04/04  NCR
2017/04/05  Oracle
2017/04/07  Pepsi Bottling
2017/04/10  Pfizer
2017/04/13  Penn National Gaming
2017/04/14  Prudential Financial
2017/04/17  RadioShack
2017/04/18  Regal Entertainment Group
2017/04/24  SanDisk
2017/04/26  Facebook
2017/05/01  Chevron
2017/05/04  AFLAC
2017/05/05  Applied Industrial Technologies
2017/05/08  Charter Communications
2017/05/09  Coca-Cola
2017/05/10  Dow Chemical
2017/05/11  Foot Locker
2017/05/15  General Dynamics
2017/05/16  Humana
2017/05/17  Imation
2017/05/19  Kimberly-Clark
2017/05/24  3M
2017/05/25  Facebook
2017/05/26  Chevron
2017/05/01  Foot Locker
2017/05/04  General Dynamics
2017/05/08  Humana
2017/05/12  Imation
2017/05/15  Kimberly-Clark
2017/05/16  3M
2017/05/18  Public Storage
2017/05/19  Qualcomm
2017/05/22  Sealy
2017/05/24  Steelcase
2017/05/26  Textron
2017/05/29  Men's Wearhouse
2017/05/30  Toll Brothers
2017/05/31  Tractor Supply
2017/06/07  USG
2017/06/09  Facebook
2017/06/13  Chevron
2017/06/14  AFLAC
2017/06/15  Applied Industrial Technologies
2017/06/19  Charter Communications
2017/06/20  Coca-Cola
2017/06/22  Dow Chemical
2017/06/23  Foot Locker
2017/06/26  General Dynamics
2017/06/28  Humana
2017/06/01  Imation
2017/06/02  Kimberly-Clark
2017/06/05  3M
2017/06/06  Facebook
2017/06/07  Chevron
2017/06/08  Foot Locker
2017/06/12  General Dynamics
2017/06/13  Humana
2017/06/14  Imation
2017/06/16  Kimberly-Clark
2017/06/20  3M
2017/06/21  Public Storage
2017/06/22  Qualcomm
2017/06/29  Sealy
2017/06/30  Steelcase
2017/07/11  Textron
2017/07/12  Men's Wearhouse
2017/07/13  Toll Brothers
2017/07/14  Tractor Supply
2017/07/19  USG
2017/07/21  Facebook
2017/07/24  Chevron
2017/07/25  AFLAC
2017/07/26  Applied Industrial Technologies
2017/07/27  Charter Communications
2017/08/01  Coca-Cola
2017/08/02  Dow Chemical
2017/08/10  Foot Locker
2017/08/11  General Dynamics
2017/08/15  Humana
2017/08/16  Imation
2017/08/17  Kimberly-Clark
2017/08/21  3M
2017/08/23  Facebook
2017/08/24  Chevron
2017/08/25  Foot Locker
2017/08/29  General Dynamics
2017/08/30  Humana
2017/08/31  Imation
2017/09/07  Kimberly-Clark
2017/09/08  3M
2017/09/13  Public Storage
2017/09/14  Qualcomm
2017/09/15  Sealy
2017/09/20  Steelcase
2017/09/21  Textron
2017/09/22  Men's Wearhouse
2017/09/25  Toll Brothers
2017/09/26  Tractor Supply
2017/09/27  USG
2017/09/04  Facebook
2017/09/05  Chevron
2017/09/06  AFLAC
2017/09/11  Applied Industrial Technologies
2017/09/13  Charter Communications
2017/09/14  Coca-Cola
2017/09/15  Dow Chemical
2017/09/20  Foot Locker
2017/09/21  General Dynamics
2017/09/22  Humana
2017/09/26  Imation
2017/09/27  Kimberly-Clark
2017/09/29  3M
2017/10/02  Facebook
2017/10/03  Chevron
2017/10/05  Foot Locker
2017/10/11  General Dynamics
2017/10/12  Humana
2017/10/16  Imation
2017/10/18  Kimberly-Clark
2017/10/19  3M
2017/10/20  Public Storage
2017/10/23  Qualcomm
2017/10/24  Sealy
2017/10/26  Steelcase

【问题讨论】:

    标签: sql-server reporting-services ssrs-2008-r2 ssrs-2012


    【解决方案1】:

    使用not exists():

    select distinct companies
    from [company].[dbo].[Contact] c
    where not exists (
      select 1
      from [company].[dbo].[Contact] i
      where i.companies = c.companies
        and i.dates >= convert(date,dateadd(day,-45,getdate()))
      )
    

    如果您有一个 companies 表,那么您可以在 contact 表上使用它而不是 distinct。这也将使您能够返回在contact 中没有条目的companies

    select *
    from [company].[dbo].[companies] c
    where not exists (
      select 1
      from [company].[dbo].[Contact] i
      where i.companies = c.companies
        and i.dates >= convert(date,dateadd(day,-45,getdate()))
      )
    

    另一个使用having() 子句聚合的选项仅返回max(dates) 小于45 天前的companies;还返回最近的联系日期:

    select 
        companies
      , LastContact = max(dates)
    from [company].[dbo].[Contact] c
    group by companies
    having max(dates)<convert(date,dateadd(day,-45,getdate()))
    

    rextester 演示:http://rextester.com/OJER22556

    返回:

    +---------------------------+-------------+
    |         companies         | LastContact |
    +---------------------------+-------------+
    | Brunswick                 | 2017-04-27  |
    | Capital One Financial     | 2017-03-28  |
    | CBS                       | 2017-03-29  |
    | Citigroup                 | 2017-04-03  |
    | E*Trade Financial         | 2017-04-05  |
    | Eastman Kodak             | 2017-04-12  |
    | El Paso                   | 2017-04-06  |
    | Goodrich                  | 2017-04-07  |
    | Hanesbrands               | 2017-04-10  |
    | International Paper       | 2017-04-11  |
    | Kraft Foods               | 2017-04-20  |
    | LSI                       | 2017-04-25  |
    | Macy's                    | 2017-04-26  |
    | Manpower                  | 2017-04-03  |
    | Meredith                  | 2017-04-28  |
    | NCR                       | 2017-04-04  |
    | Oracle                    | 2017-04-05  |
    | Penn National Gaming      | 2017-04-13  |
    | Pepsi Bottling            | 2017-04-07  |
    | Pfizer                    | 2017-04-10  |
    | Prudential Financial      | 2017-04-14  |
    | RadioShack                | 2017-04-17  |
    | Regal Entertainment Group | 2017-04-18  |
    | SanDisk                   | 2017-04-24  |
    | Union Pacific             | 2017-03-08  |
    | Valspar                   | 2017-03-01  |
    | Waste Management          | 2017-03-03  |
    | Williams-Sonoma           | 2017-03-06  |
    | Yahoo                     | 2017-03-07  |
    +---------------------------+-------------+
    

    【讨论】:

      【解决方案2】:
      declare @date datetime = getdate()
      
      select * from [company].[dbo].[contact] where DATEDIFF(DAY, dates, @date) >= 45
      

      由于对过滤谓词执行操作对性能不利(防止使用索引等),因此性能更高的方法是:

      declare @date datetime = getdate()
      
      select
          id
          ,dates
          ,company
      from (
          select 
              *,
              DATEDIFF(DAY, dates, @date) as daysSinceContact
          from 
              [company].[dbo].[contact]
      ) as c
      where
          daysSince >= 45
      

      或使用 CTE

      declare @date datetime = getdate()
      
      ;with c as (
          select 
              *,
              DATEDIFF(DAY, dates, @date) as daysSinceContact
          from 
              [company].[dbo].[contact]
      )
      
      select
          id
          ,dates
          ,company
      from 
          c
      where
          daysSince >= 45
      

      【讨论】:

      • 您好,非常感谢您的回复,但是,我在 SQL Server Msg 102, Level 15, State 1, Line 1 '=' 附近出现错误语法。 Msg 137, Level 15, State 2, Line 10 必须声明标量变量“@date”。
      • @AfricanHeart 很抱歉,愚蠢的我,我忘记了变量类型(还没有咖啡)!答案已更新。
      • 这两种方法在性能方面没有区别; CTE 作为查询执行。 stackoverflow.com/questions/706972/…
      • @paolo 我的意思是在第一个查询和第二个查询之间。不是派生表与 CTE。虽然如果我们在这里要求严格。在某些情况下,派生表的 CTE 性能会更高。
      • 如果您将右侧的计算转移到日期,性能可能会更好>=DATEADD(day, DATEDIFF(day, 0, GETDATE()), -45)
      【解决方案3】:
      SELECT Companies
          ,[Dates]
          ,DATEDIFF(Day, [Dates], CAST(Getdate() AS DATE)) AS [DaysHaven't Been Contacted Past45 days]
      FROM #Temp o
      WHERE DATEDIFF(Day, [Dates], CAST(Getdate() AS DATE)) >= 45
      ORDER BY [Dates] DESC
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-14
        • 2020-11-05
        • 2013-07-08
        • 2016-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多