最有效的方法来找到最昂贵的近期订单

时间:2017-10-24 05:35:23

标签: sql-server-2008

我正在努力寻找客户最近和最昂贵的购买。我有四个表客户,订单,订单详细信息和产品代码。所有帐户都是emailaddress独有的。

错误无法绑定多部分标识符“cx.EmailAddress”

SELECT c.EmailAddress,
       o.BillingFirstName AS WC_FirstName,
       o2.LatestOrder
FROM Customers c
JOIN orders o ON o.customerid = c.customerid
JOIN( SELECT
            cx.EmailAddress,
            MAX(o.OrderID) AS LatestOrder
      FROM Customers AS cx
      JOIN Orders o ON o.customerid = cx.customerid
      WHERE o.OrderStatus <> 'CANCELLED' AND o.OrderDate > '09/01/2017 00:00'
      GROUP BY EmailAddress) AS o2 on o2.EmailAddress = cx.EmailAddress
 GROUP BY c.EmailAddress,o.BillingFirstName

UPDATE也以另一种方式尝试但仍然出错:无法绑定多部分标识符“c.EmailAddress”。

SELECT 
    c.EmailAddress,
    c.CustomerID,
    o.OrderDate,
    p.Google_Gender,
    p.Google_Age_Group ,
    p.productprice AS Product_Price
FROM 
    Customers c
JOIN 
    orders o ON o.CustomerID = c.CustomerID
JOIN
    (SELECT 
         c.EmailAddress,
         MAX(o.OrderID) AS LatestOrder
     FROM
         Orders o, Customers c 
     WHERE
         o.OrderStatus <> 'CANCELLED' AND
         o.CustomerID = c.CustomerID
     GROUP BY
         c.EmailAddress) AS o2 ON o2.EmailAddress = c.EmailAddress
JOIN
    (SELECT 
         od.*,
         c.EmailAddress,
         row_number() over (partition BY c.EmailAddress
                            ORDER BY od.ProductPrice DESC, o.OrderDate DESC)  AS seqnum
     FROM 
         OrderDetails od
     JOIN 
         Orders o ON od.OrderID = o.OrderID
     JOIN 
         Customers c ON o.CustomerID = c.CustomerID
     JOIN
         (SELECT 
              c.EmailAddress,
              MAX(o.OrderID) AS LatestOrder
          FROM
              Orders o , Customers c
          WHERE
              o.OrderStatus <> 'CANCELLED' AND
              o.CustomerID = c.CustomerID
          GROUP BY
              c.EmailAddress) AS o2 ON o2.EmailAddress = c.EmailAddress
    WHERE 
        o.OrderID = o2.LatestOrder) od ON od.CustomerID = c.CustomerID
                                       AND seqnum = 1
JOIN 
    Products_Joined p ON od.ProductCode = p.ProductCode
FULL JOIN 
    (SELECT p.ProductCode, p.ProductName FROM Products_Joined AS p) AS p2 ON p2.ProductCode = p.Google_Age_Group
WHERE
    AND o.PaymentAmount <> 0
    AND o.OrderID = o2.LatestOrder
GROUP BY 
    c.EmailAddress, o.OrderDate, c.CustomerID, p.productprice,
    p.Google_Age_Group, p.Google_Gender, p.Google_Pattern,
    p.Google_Size, od.ProductName, p.ProductName, p2.productname,
    p.productcode
ORDER BY 
    o.OrderDate DESC, MAX(od.ProductPrice) DESC;

1 个答案:

答案 0 :(得分:1)

您有错误,因为cxo2查询中的内部别名。所以你应该改变这个的JOIN条件:

AS o2 on o2.EmailAddress = c.EmailAddress

这样您就可以直接将o2电子邮件地址加入来自客户的电子邮件地址。

仅仅为了提高性能,如果您按照CustomerID加入客户会更好,就像您在第一次加入时一样,而不是通过电子邮件加入。

最后,您正在执行OrderID的MAX,这很可能会返回最近的订单。考虑到这一点,您应该将查询更改为类似的内容(我猜测Amount是该字段的名称):

SELECT c.EmailAddress,
       o.BillingFirstName AS WC_FirstName,
       MostExpensiveOrderId = o.OrderId,
       MostExpensiveOrderAmount = o.Amount
FROM Customers c
INNER JOIN (
    SELECT
        CustomerID,
        MAX(Amount)
    FROM Orders
    WHERE OrderStatus <> 'CANCELLED' AND OrderDate > '09/01/2017 00:00'
    GROUP BY CustomerID
) AS omax ON omax.CustomerID = C.CustomerID
INNER JOIN Orders o ON o.CustomerID = c.CustomerID AND o.Amount = omax.Amount
ORDER BY c.EmailAddress, o.BillingFirstName

请注意,您不需要内部查询中的Customers表。另请注意,如果最高值与多个订单共享,则每个客户可能会获得多行。