最近的订单最贵

时间:2017-09-20 17:35:49

标签: sql sql-server

我正在使用SQL Server 2008.我正在进行此查询,我希望通过最近的购买获得最昂贵的订单。所有帐户都是emailaddress的唯一帐户,而不是customerid。我收到以下错误:

  

多部分标识符" 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 WITH (NOLOCK), Customers c WITH (NOLOCK)
     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 WITH (NOLOCK), Customers c WITH (NOLOCK)
          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
    o.OrderDate >= (getdate()-40)
    AND od.ProductPrice NOT LIKE '0'
    AND o.OrderStatus NOT IN ('Cancelled', 'Payment Declined')
    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;

我无法弄清楚我错过的连接?或者为什么我收到此错误?任何帮助表示赞赏。

select 
     c.EmailAddress
   , x.CustomerID
   , x.OrderDate
   , x.Google_Gender
   , x.Google_Age_Group
   , x.productprice
,CASE WHEN x.google_pattern IS NULL THEN x.ProductName ELSE x.Google_Pattern END as SO_Purchase_Link
,CASE WHEN x.google_age_group IS NULL THEN x.Google_Pattern ELSE p2x.ProductName END AS SO_Purchase_ALTLIST
,CASE WHEN x.google_age_group IS NULL THEN x.ProductCode ELSE p.google_age_group END AS ProductCode
,MAX(x.ProductPrice) AS expensiveorder
,CASE WHEN x.google_age_group IS NULL THEN 'Config_FullStoreURLProductDetails.asp?ProductCode=' + x.productcode ELSE 'Config_FullStoreURLProductDetails.asp?ProductCode=' + p.google_age_group END AS 'SO_PURCHASED_LINK'
,CASE WHEN x.google_age_group IS NULL THEN 'Config_FullStoreURLConfig_ProductPhotosFolder/' + x.productcode + '-2T.jpg' ELSE 'Config_FullStoreURLConfig_ProductPhotosFolder/' +x.google_age_group + '-2T.jpg' END AS 'SO_Purchased Image'
from (
  select distinct EmailAddress 
  from Customers with (nolock)
  ) c
  cross apply (
    select top 1 
       xc.CustomerId
      , o.OrderDate
      , p.Google_Gender
,p.google_pattern
,p.ProductName
      , p.Google_Age_Group
      , p.ProductPrice
    from Customers xc with (nolock)
      inner join orders o with (nolock)
        on o.CustomerID = xc.CustomerID
      inner join OrderDetails od with (nolock)
        on od.OrderID = o.OrderID
      inner join Products_Joined p with (nolock)
        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 xc.EmailAddress = c.EmailAddress -- one per unique email address
      and o.OrderDate >= (getdate() - 40)
      and o.PaymentAmount <> 0 
      and od.ProductPrice <> 0
      and o.OrderStatus not in ('Cancelled', 'Payment Declined') 
    order by od.ProductPrice desc,o.OrderDate desc
  ) x

错误:The multi-part identifier "p2x.ProductName" could not be bound.

1 个答案:

答案 0 :(得分:1)

使用cleanup:可能会让事情变得更容易阅读:

cross apply()

注意: