在新客户和返回客户SQL中更改日期范围时删除重复项

时间:2017-08-24 18:42:27

标签: mysql sql sql-server-2008

  

我正在检查新客户和重复客户,为期三个月。查询在我运行任何一个月时都能正常运行,但是当看到2-3个月时它会向我显示重复的电子邮件地址。

示例数据(显示错误)

month emailaddress      first_order       codate       customertype
2    xyz@hotmail.com     10/27/2015       01/10/2017     Repeat
3    xyz@hotmail.com     10/27/2015       01/10/2017     Repeat

所以基本上这个顾客在jan,feb和march购买了他的第一次购买是在2015年。所以这是正确的他是一个重复的客户,但在最终输出我只想看到他: month emailaddress first_order codate customertype 1 xyz@hotmail.com 10/27/2015 01/10/2017重复

请记住,要唯一地定义客户,我们必须使用客户电子邮件,而不是客户ID(因为它不是唯一的)

任何帮助将不胜感激。这是一个完美的单个月但不是多个的代码。

SELECT 
A.MONTH,
A.emailAddress,
A.FIRST_ORDER_EMAIL as first_order,
A.first_order as codate,
CASE WHEN CAST(A.first_order AS DATE)  =  CAST(A.FIRST_ORDER_EMAIL AS DATE) THEN 'New' ELSE 'Repeat' END customerType
FROM
(
SELECT
DATEPART(mm, s.OrderDate) AS MONTH,
c.emailAddress,
 (SELECT min(o.OrderDate)
FROM Orders o
WHERE c.CustomerID=o.CustomerID
AND o.OrderDate BETWEEN '01/01/2017 00:00' AND '03/31/2017 23:59' AND o.OrderStatus NOT IN ('cancelled','Payment Declined','Returned'))AS first_order,
 (SELECT min(o.OrderDate)
FROM Orders o
INNER JOIN customers CO
ON CO.customerid = o.customerid 
WHERE c.emailAddress=CO.emailAddress
AND o.OrderDate BETWEEN '1/1/2010 00:00' AND '03/31/2017 23:59'
AND o.OrderStatus NOT IN ('cancelled','Payment Declined','Returned')) AS FIRST_ORDER_EMAIL
FROM orders s
JOIN orders p ON p.CustomerID = s.CustomerID
AND p.OrderID <= s.OrderID
JOIN customers c ON c.CustomerID = s.CustomerID
JOIN OrderDetails od ON od.OrderID = s.OrderID
WHERE 
 s.OrderDate BETWEEN '01/01/2017 00:00' AND '03/31/2017 23:59'
AND s.OrderStatus NOT IN ('cancelled','Payment Declined','Returned')
AND od.ProductPrice <> 0
AND od.ProductCode = 'xyz'
GROUP BY c.emailAddress,
c.CustomerID,
DATEPART(mm, s.OrderDate),
s.OrderDate
)A
GROUP BY 
A.MONTH,
A.emailAddress,
A.FIRST_ORDER_EMAIL,
A.first_order,
CASE WHEN CAST(A.first_order AS DATE)  =  CAST(A.FIRST_ORDER_EMAIL AS DATE) THEN 'New' ELSE 'Repeat' END

1 个答案:

答案 0 :(得分:0)

尝试使用ROW_NUMBER()来确定每个电子邮件地址发生的第一条记录

WITH result AS
( SELECT a.[month],
         a.emailaddress,
         a.first_order_email AS first_order,
         a.first_order AS codate,
         CASE WHEN CAST(a.first_order AS DATE)  =  CAST(a.first_order_email AS DATE)
              THEN 'New' 
              ELSE 'Repeat' 
          END customerType,
         ROW_NUMBER() OVER (PARTITION BY a.emailaddress ORDER BY a.[month]) AS rnum
   FROM (SELECT DATEPART(mm, s.orderdate) AS [month],
                c.emailaddress,
                (SELECT min(o.orderdate)
                   FROM orders o
                  WHERE c.customerid = o.customerid
                    AND o.orderdate BETWEEN '01/01/2017 00:00' AND '03/31/2017 23:59' 
                    AND o.orderstatus NOT IN ('cancelled','Payment Declined','Returned'))AS first_order,
                (SELECT min(o.orderdate)
                   FROM orders o
                  INNER JOIN customers co
                     ON co.customerid = o.customerid 
                  WHERE c.emailaddress = co.emailaddress
                    AND o.orderdate BETWEEN '1/1/2010 00:00' AND '03/31/2017 23:59'
                    AND o.orderstatus NOT IN ('cancelled','Payment Declined','Returned')) AS first_order_email
           FROM orders s
           JOIN orders p 
             ON p.customerid = s.customerid
            AND p.orderid <= s.orderid
           JOIN customers c 
             ON c.customerid = s.customerid
           JOIN orderdetails od 
             ON od.orderid = s.orderid
          WHERE s.orderdate BETWEEN '01/01/2017 00:00' AND '03/31/2017 23:59'
            AND s.orderstatus NOT IN ('cancelled','Payment Declined','Returned')
            AND od.ProductPrice <> 0
            AND od.ProductCode = 'xyz'
          GROUP BY c.emailaddress,
                   c.customerid,
                   DATEPART(mm, s.orderdate),
                   s.orderdate
        ) a
  GROUP BY a.[month],
           a.emailaddress,
           a.first_order_email,
           a.first_order,
           CASE WHEN CAST(a.first_order AS DATE)  =  CAST(a.first_order_email AS DATE) 
                THEN 'New' 
                ELSE 'Repeat' 
            END
)

SELECT [month],
       emailaddress,
       first_order,
       codate,
       customerType
  FROM result
 WHERE rnum = 1