SQL Server在每个客户的日期内计算订单

时间:2016-10-11 19:57:53

标签: sql-server tsql

我试图获取以下数据:

  • 列出在过去12个月内订购两次或更多次的所有客户
  • 列出过去12个月内仅订购一次的所有客户
  • 列出任何不符合上述条件的客户

我使用的是SQL Server 11.0。

我有以下表格:

DBO [订购]

CustomerID
OrderID

dbo.Customer

BusinessName
Postcode

dbo.AccountCallbacks

UserId
NotInterestedReasonID

我需要计算来自dbo的所有订单。[订单]其中dbo.AccountCallbacks中的最后一条记录的用户ID为' 6EAE3206-519E-4DE7-B10B-6F2476D7D20F'以及上述每个日期范围之间的null NotInterestedReasonID,然后是其他所有日期范围。

我不确定要提供什么其他信息,这是我目前提出的,但我觉得我是以一种特殊的方式进行的!

SELECT
    Customer.CustomerID,
    BusinessName,
    Postcode,
    NumOrders
FROM  
    Customer
INNER JOIN 
    (SELECT
         CustomerID,
         COUNT(OrderID) AS NumOrders
     FROM 
         dbo.[Order]
     WHERE 
         UserId = '6EAE3206-519E-4DE7-B10B-6F2476D7D20F'
         AND NOT (PaymentDate IS NULL)
         AND OrderDate <= DATEADD(MONTH, -12, GETDATE())
     GROUP BY 
         CustomerID) AS payingCustomers ON Customer.CustomerID = payingCustomers.CustomerID
INNER JOIN 
    (SELECT
         CustomerID,
         MAX(CallbackDate) AS LastCallbackDate
     FROM 
         dbo.AccountCallBacks
     WHERE 
         NotInterestedReasonID IS NULL
     GROUP BY 
         CustomerID) AS otherCustomers ON Customer.CustomerID = otherCustomers.CustomerID
ORDER BY 
    NumOrders DESC

正如您可能已经猜到的那样,SQL并不是我最强的诉讼!我真的希望我已经提供了所需信息,如果没有,请告诉我。

示例数据:

AccountCallbacks:

CallbackID  UserID  CustomerID  Created CallbackDate    Enabled CallbackTimeID  NotInterestedReasonID
16  695624B5-90E0-45C0-AFCF-07C7A275BE6E    504 2011-02-01 10:40:37.183 2015-10-08  1   3   1
17  695624B5-90E0-45C0-AFCF-07C7A275BE6E    505 2011-02-01 10:40:37.220 2011-11-01  0   3   NULL
18  2B37842F-33AF-4777-9FC7-3D4F648F5D8F    506 2011-02-01 10:40:37.263 2012-08-20  0   1   NULL
19  2B37842F-33AF-4777-9FC7-3D4F648F5D8F    508 2011-02-01 10:40:37.300 2012-07-20  0   1   NULL
20  2B37842F-33AF-4777-9FC7-3D4F648F5D8F    509 2011-02-01 10:40:37.340 2014-02-10  0   1   NULL

订单:

OrderID CustomerID  UserID  OrderDate   PaymentTypeID   PaymentStatusID PaymentDate TransactionRef  PurchaseOrderNumber
44523   4199    695624B5-90E0-45C0-AFCF-07C7A275BE6E    2016-10-11 16:54:01.350 1   2   2016-10-11 16:57:13.000 011194  
44522   3748    695624B5-90E0-45C0-AFCF-07C7A275BE6E    2016-10-11 16:13:00.290 1   2   2016-10-11 16:13:57.000 011486  
44521   1812    2B37842F-33AF-4777-9FC7-3D4F648F5D8F    2016-10-11 16:08:16.923 1   2   2016-10-11 16:09:33.000 082663  

客户:

CustomerID  BusinessName    Postcode
502 Company 1   BP3 6UK
503 Company Name    BP3 6BK
504 Company ABC SS13 1LS

预期输出:(这将是一组不同的数据,具体取决于报告&#39;报告如上所述)

CustomerID  BusinessName    Postcode
799 Company2 LTD    YO17 6YA    
5586    Company3 Plc    EH3 9DJ 
5638    MR A ENG LTD    EP4 1PL 
6707    DUSTO Ltd   NE22 7LB    

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

列出过去12个月内订购两次或更多次的所有客户

SELECT COUNT(o.OrderID), o.CustomerID
FROM Order o 
WHERE o.UserID = '6EAE3206-519E-4DE7-B10B-6F2476D7D20F'
    AND o.OrderDate > DATEADD(MONTH, -12, GETDATE())
GROUP BY o.CustomerID
HAVING COUNT(o.OrderID) > 2

列出过去12个月内仅订购一次的所有客户

SELECT COUNT(o.OrderID), o.CustomerID
FROM Order o 
WHERE o.UserID = '6EAE3206-519E-4DE7-B10B-6F2476D7D20F'
    AND o.OrderDate > DATEADD(MONTH, -12, GETDATE())
GROUP BY o.CustomerID
HAVING COUNT(o.OrderID) = 1

我对这个问题有点困惑。您希望所有“不符合标准的客户”。什么标准?