SQL将STUFF函数添加到此查询中

时间:2016-05-20 10:04:16

标签: sql sql-server

我有以下查询

SELECT TOP 1000 
    o.BuyerEMail,
    COUNT(*) HowMany,
    o.Name, o2.OrderID
FROM 
    Orders o
JOIN 
    (SELECT 
         BuyerEmail, MAX(OrderDate) Latest 
     FROM Orders 
     GROUP BY BuyerEmail) l ON o.BuyerEmail = l.BuyerEmail
JOIN 
    Orders o2 ON l.BuyerEmail = o2.BuyerEmail
              AND l.OrderDate = o2.OrderDate
WHERE 
    Pay != 'PayPal'
GROUP BY 
    o.BuyerEmail, o.Name, l.Latest
ORDER BY 

它只是制作一份关于客户的报告,以及他们与我们订购了多少订单以及最新的订单。

我想要添加的是最后一个订单的产品列表,这些产品位于行表中。

行表由OrderID链接到订单表。

我正在寻找类似的东西:

JOIN
    (SELECT 
         OrderID,
         STUFF((SELECT ', ' + li.Code
                FROM tblLines li
                WHERE li.OrderID = o2.OrderID 
                FOR XML PATH ('')), 1, 1, '') AS [Codes]
GROUP BY
    OrderID, Code

所以决赛桌显示为

BuyerEmail | HowMany | Name | Latest | Codes
---------------------------------------------
Bob@bob    | 4       | bob  | 10000  | 123,10,201231

2 个答案:

答案 0 :(得分:3)

当您从其他问题(SQL most recent order? MS SQL)发布查询时,我将使用我的答案,因为它比上述查询更清晰:

SELECT o.*
, OrderID as LastOrderID
FROM (
    SELECT BuyerEMail
    , Name
    , COUNT(*) as TotalOrders
    FROM Orders
    WHERE Pay != 'PayPal'
    GROUP BY BuyerEmail, Name
) o
CROSS APPLY (
    SELECT TOP 1 OrderID, OrderDate
    FROM Orders s
    WHERE s.BuyerEmail = o.BuyerEmail
    ORDER BY OrderDate DESC
) ca

你发布了一个很好的例子,但你并没有完成。您需要以下xmlpath查询:

SELECT OrderID
, Codes
FROM tblLines r1
CROSS APPLY ( 
    SELECT
        STUFF((SELECT ',' + CAST(Code AS NVARCHAR)
        FROM tblLines r2
        WHERE r2.OrderID = r1.OrderID
        GROUP BY OrderID, Code
        ORDER BY Code
        FOR XML PATH (''), TYPE)
        .value('.', 'varchar(max)')
        , 1, 1, ''))  OrderLines(Codes)
GROUP BY OrderID, OrderList

使用简单的连接将其添加到上一个语句:

SELECT  o.BuyerEMail
    ,o.Name
    ,o.TotalOrders
    , OrderID as LastOrderID
    , c.Codes
FROM (
        SELECT BuyerEMail
        , Name
        , COUNT(*) as TotalOrders
        FROM Orders
        WHERE Pay != 'PayPal'
        GROUP BY BuyerEmail, Name
) o
CROSS APPLY (
        SELECT TOP 1 OrderID, OrderDate
        FROM Orders s
        WHERE s.BuyerEmail = o.BuyerEmail
        ORDER BY OrderDate DESC
) ca 
INNER JOIN (
    SELECT OrderID
    , Codes
    FROM tblLines r1
    CROSS APPLY ( 
        SELECT
            STUFF((SELECT ',' + CAST(Code AS NVARCHAR)
            FROM tblLines r2
            WHERE r2.OrderID = r1.OrderID
            GROUP BY OrderID, Code
            ORDER BY Code
            FOR XML PATH (''), TYPE)
            .value('.', 'varchar(max)')
            , 1, 1, ''))  OrderLines(Codes)
    GROUP BY OrderID, OrderList
) c
ON ca.OrderID = c.OrderID

答案 1 :(得分:0)

SELECT TOP 1000 
o.BuyerEMail,
COUNT(*) HowMany,
o.Name, l.Latest,
STUFF((SELECT ', ' + li.Code
            FROM tblLines li
            WHERE li.OrderID = o2.OrderID 
            FOR XML PATH ('')), 1, 1, '') AS [Codes]

这     订单o 加入     (选择          BuyerEmail,MAX(OrderDate)最新      来自订单      GROUP BY BuyerEmail)l ON o.BuyerEmail = l.BuyerEmail 加入     订单o2 ON l.BuyerEmail = o2.BuyerEmail               AND l.OrderDate = o2.OrderDate 哪里     付!='PayPal' 通过...分组     o.BuyerEmail,o.Name,l。最新 订购