用于查找在给定月份内订购特定产品的新客户和重复客户的SQL

时间:2017-08-22 16:51:35

标签: sql-server sql-server-2008 tsql

SELECT
DATEPART(mm, o.OrderDate) AS Month,
c.EmailAddress,
od.productcode AS SKU,
SUM(od.Quantity) as Quantity,
SUM((od.ProductPrice) * (od.Quantity) ) AS REVENUE,
(select count(o.OrderID) FROM Orders o WHERE c.CustomerID=o.CustomerID AND o.OrderDate BETWEEN  '1/1/2010 00:00' AND getdate()) AS NO_OF_ORDERS,
(CASE WHEN exists (Select * from Orders 
       where CustomerID = c.CustomerId
          and orderDate < o.orderDate) 
       then 'Repeat' else 'New' end ) AS Customers
FROM Customers c
    join (Orders o join OrderDetails od
            on od.OrderID = o.OrderID)
        on o.CustomerID = c.CustomerID 
WHERE
o.OrderDate BETWEEN '1/1/17 00:00' AND '01/31/17 23:59'
        AND o.OrderStatus <> 'Cancelled'
        AND od.ProductPrice <> 0
        AND od.ProductCode = 'CBTN850'
        AND od.Quantity NOT LIKE 0
GROUP BY DATEPART(mm,o.OrderDate),c.EmailAddress, od.productcode,c.CustomerID,o.OrderDate

立即开展此工作

 select Customers.EmailAddress,
    case when( c1.EmailAddress=Customers.EmailAddress then new  end) as customers
    from
    (SELECT
    EmailAddress,
    MIN(Orders.OrderDate) as FirstOrderdate
    FROM
    Customers
    INNER JOIN
    Orders
    ON Orders.CustomerID = Customers.CustomerID
    JOIN OrderDetails
    ON OrderDetails.OrderID = Orders.OrderID
    WHERE Orders.OrderDate > '01/01/2017 00:00:00' 
     AND Orders.OrderDate <  '01/31/2017 00:00:00'
     AND Orders.OrderStatus <> 'Cancelled'
            AND OrderDetails.ProductPrice <> 0
            AND OrderDetails.ProductCode = 'CBTN850'
            AND OrderDetails.Quantity NOT LIKE 0
          AND Customers.EmailAddress NOT IN (
              SELECT Customers.EmailAddress FROM Customers
               INNER JOIN
               Orders
               ON Orders.CustomerID = Customers.CustomerID
        WHERE Orders.OrderDate< '01/01/2017 00:00:00')c1 AS c1.EmailAddress=Customers.EmailAddress
    GROUP BY  Customers.EmailAddress

尝试执行SQL查询,显示给定月份的新客户和返回客户。新客户是从未订购过的客户。

    SELECT
        c.EmailAddress,
        od.productcode AS SKU,
        COUNT(o.OrderID) AS number_of_Orders,
        DATEPART(mm, o.OrderDate) AS Month
    FROM
        Customers c
        JOIN Orders o
            ON c.CustomerID=o.CustomerID
        JOIN OrderDetails od
            ON od.OrderID = o.OrderID
    WHERE
        o.OrderDate BETWEEN '1/1/17 00:00' AND '04/30/17 23:59'
            AND o.OrderStatus <> 'Cancelled'
            AND od.ProductPrice <> 0
            AND od.ProductCode = 'ABC01'
            AND od.Quantity NOT LIKE 0
    GROUP BY 
        DATEPART(mm,o.OrderDate),
        c.EmailAddress,
        od.productcode

我写了这段代码。但这不是我希望它的工作方式。我想看看 产品代码,新客户和重复客户作为输出。我们可以为新客户和重复客户制作两个案例吗?

3 个答案:

答案 0 :(得分:0)

是的,你可以用两个案例来做。

这是伪代码:

SUM(CASE WHEN {Customer has other orders} THEN 1 ELSE 0 END) AS ReturningCustomers,
SUM(CASE WHEN {Customer has no other orders} THEN 1 ELSE 0 END) AS NewCustomers

答案 1 :(得分:0)

试试这个:

SELECT DATEPART(mm, o.OrderDate) Month,
   c.EmailAddress, od.productcode SKU,
   case when exists 
      (Select * from Orders 
       where CustomerID = c.CustomerId
          and orderDate < o.orderDate) 
       then 'Repeat' else 'New' end NewCustomer,
   COUNT(o.OrderID) Orders

FROM Customers c
    join (Orders o join OrderDetails od
            on od.OrderID = o.OrderID)
        on o.CustomerID = c.CustomerID 

WHERE o.OrderDate BETWEEN '1/1/17 00:00' AND '04/30/17 23:59'
    AND o.OrderStatus <> 'Cancelled'
    AND od.ProductPrice <> 0
    AND od.ProductCode = 'ABC01'
    AND od.Quantity NOT LIKE 0

GROUP BY DATEPART(mm,o.OrderDate), c.EmailAddress, od.productcode

如果这会导致group by error,则将子查询表达式添加到group by子句中:

SELECT DATEPART(mm, o.OrderDate) Month,
   c.EmailAddress, od.productcode SKU,
   case when exists 
      (Select * from Orders 
       where CustomerID = c.CustomerId
          and orderDate < o.orderDate) 
       then 'Repeat' else 'New' end NewCustomer
   COUNT(o.OrderID) Orders,

FROM Customers c
    join (Orders o join OrderDetails od
            on od.OrderID = o.OrderID)
        on o.CustomerID = c.CustomerID 

WHERE o.OrderDate BETWEEN '1/1/17 00:00' AND '04/30/17 23:59'
    AND o.OrderStatus <> 'Cancelled'
    AND od.ProductPrice <> 0
    AND od.ProductCode = 'ABC01'
    AND od.Quantity NOT LIKE 0

GROUP BY DATEPART(mm,o.OrderDate), c.EmailAddress, od.productcode,
     case when exists 
      (Select * from Orders 
       where CustomerID = c.CustomerId
          and orderDate < o.orderDate) 
       then 'Repeat' else 'New' end

答案 2 :(得分:0)

尝试下面的内容(希望它有所帮助或给你一个想法)

获取包含每个客户ID和状态的ProductCode

with ordersData as
(select o.customerId,od.ProductCode,count(distinct o.orderId)ordersCount from  Orders o         
        inner join OrderDetails od
            ON od.OrderID = o.OrderID
    WHERE
        o.OrderDate BETWEEN '1/1/17 00:00' AND '04/30/17 23:59'
            AND o.OrderStatus <> 'Cancelled'
            AND od.ProductPrice <> 0
            AND od.ProductCode = 'ABC01'
            AND od.Quantity <> 0
  group by o.customerId,od.ProductCode
)

,RepeatedCustomersT as
(select customerId,ProductCode,'Repeat' as CustomerStatus from ordersData where ordersCount>1)

,NewCustomersT as
(select customerId,ProductCode , 'New' as CustomerStatus from ordersData where ordersCount=1)

select  ProductCode ,customerId,CustomerStatus as RepeatedCustomersT
union all
select  ProductCode ,customerId,CustomerStatus as NewCustomersT

但如果您想获得输出Like(Productcode - RepeatedCustomer- NewCustomers),您可以尝试以下查询

with ordersData as
(select o.customerId,od.ProductCode,count(distinct o.orderId)ordersCount from  Orders o         
        inner join OrderDetails od
            ON od.OrderID = o.OrderID
    WHERE
        o.OrderDate BETWEEN '1/1/17 00:00' AND '04/30/17 23:59'
            AND o.OrderStatus <> 'Cancelled'
            AND od.ProductPrice <> 0
            AND od.ProductCode = 'ABC01'
            AND od.Quantity <> 0
  group by o.customerId,od.ProductCode
),
RepeatedCustomersT as
(
select customerId,ProductCode,ROW_NUMBER() over (partition by ProductCode order by customerId) rn  from ordersData where ordersCount>1
)
,NewCustomersT as
(
select customerId,ProductCode,ROW_NUMBER() over (partition by ProductCode order by customerId) rn from ordersData where ordersCount=1
)
select  ProductCode ,r.customerId as RepeatedCustomers, n.CustomerId as NewCustomers
from RepeatedCustomersT r full outer join NewCustomersT n on r.ProductCode=n.ProductCode and r.rn=n.rn