将多行合并为一列以显示在另一个查询结果中?

时间:2014-12-18 21:49:40

标签: sql sql-server subquery

我试图为我们的商店进行一般报告查询,在那里我们可以看到所有销售以及销售所在的商店。这是我迄今为止构建的查询:

SELECT      Customer.LastName + ', ' + Customer.FirstName AS 'Customer Name',
            Store.Name AS 'Store', 
            Item.ItemCode + ': ' + Item.Name AS 'Item', 
            Sale.SaleID AS 'Sale ID',
            Sale.SaleDate 'Date',
            Sale.SubTotal AS 'Subtotal', 
            Sale.GST AS 'GST'
            Sale.SaleTotal AS 'Sale Total'
FROM        Sale
            INNER JOIN Customer ON Sale.CustomerID = Customer.CustomerID
            INNER JOIN Store ON Sale.StoreID = Store.StoredID
            INNER JOIN SaleItem ON SaleItem.SaleNumber = Sale.SaleNumber
            INNER JOIN Item ON Item.ItemCode = SaleItem.ItemCode
WHERE       Sale.SaleDate >= DATEADD(MONTH, -1, GETDATE()) 
ORDER BY    Sale.SaleDate DESC

现在,上述查询的结果看起来像这样(我将它放在代码格式中,因为stackoverflow不允许表):

<table>
        <tr>
            <td>Customer Name</td>
            <td>Store</td>
            <td>Item</td>
            <td>Sale ID</td>
            <td>Date</td>
            <td>Subtotal</td>
            <td>GST</td>
            <td>Sale Total</td>
        </tr>
        <tr>
            <td>John Smith</td>
            <td>Store No. 1</td>
            <td>001: Item ABC</td>
            <td>1</td>
            <td>2014-12-18</td>
            <td>100.00</td>
            <td>5.00</td>
            <td>105.00</td>
        </tr>
        <tr>
            <td>John Smith</td>
            <td>Store No. 1</td>
            <td>002: Item DEF</td>
            <td>1</td>
            <td>2014-12-18</td>
            <td>100.00</td>
            <td>5.00</td>
            <td>105.00</td>
        </tr>
        <tr>
            <td>John Smith</td>
            <td>Store No. 1</td>
            <td>003: Item GHI</td>
            <td>1</td>
            <td>2014-12-18</td>
            <td>100.00</td>
            <td>5.00</td>
            <td>105.00</td>
        </tr>
    </table>

所以我真正想做的是将这些销售商品组合成一列,这样就可以显示出来,例如:

<table>
        <tr>
            <td>Customer Name</td>
            <td>Store</td>
            <td>Item</td>
            <td>Sale ID</td>
            <td>Date</td>
            <td>Subtotal</td>
            <td>GST</td>
            <td>Sale Total</td>
        </tr>
        <tr>
            <td>John Smith</td>
            <td>Store No. 1</td>
            <td>001: Item ABC; 002: Item DEF; 003: Item GHI</td>
            <td>1</td>
            <td>2014-12-18</td>
            <td>100.00</td>
            <td>5.00</td>
            <td>105.00</td>
        </tr>
    </table>

老实说,我不知道如何做到这一点,因为我还是相当新的SQL(只做了1。5年),但我相当肯定这需要某种子查询。< / p>

另外,作为一个说明,这个实际结果会更大,因为它将包含本月的所有不同销售额。因此,我希望将该销售中每笔销售的销售项目结合起来。

任何帮助表示赞赏!谢谢!

编辑:我正在使用SQL Server

EDIT2:我也试过像这样使用XML PATH:

SELECT      Customer.LastName + ', ' + Customer.FirstName AS 'Customer Name',
            Store.Name AS 'Store', 
            (SELECT 
                    STUFF((SELECT '; ' + Item.ItemCode + ': ' + Item.Name
                          FROM  Item
                                INNER JOIN SaleItem ON SaleItem.ItemCode = Item.ItemCode
                          WHERE SaleItem.SaleID = Sale.SaleID
                          FOR XML PATH('')), 1, 1, '') [Item Code/Name]
                FROM    SaleItem
                        INNER JOIN Sale ON SaleItem.SaleID = Sale.SaleID
                WHERE   Sale.SaleID = SaleItem.SaleID
                GROUP BY Sale.SaleID, SaleItem.SaleID) AS 'Item', 
            Sale.SaleID AS 'Sale ID',
            Sale.SaleDate 'Date',
            Sale.SubTotal AS 'Subtotal', 
            Sale.GST AS 'GST'
            Sale.SaleTotal AS 'Sale Total'
FROM        Sale
            INNER JOIN Customer ON Sale.CustomerID = Customer.CustomerID
            INNER JOIN Store ON Sale.StoreID = Store.StoredID
            INNER JOIN SaleItem ON SaleItem.SaleNumber = Sale.SaleNumber
            INNER JOIN Item ON Item.ItemCode = SaleItem.ItemCode
WHERE       Sale.SaleDate >= DATEADD(MONTH, -1, GETDATE()) 
ORDER BY    Sale.SaleDate DESC

但是,我在那里制作的子查询返回了每次销售的所有销售商品的组合。所以它有点像这样:

第1行:001:项目ABC; 002:项目DEF; 003:项目GHI
第2行:005:项目MNO; 006:项目PQR; 007:项目STU
ROW3 ...
ROW4 ...
ROW5 ...等。

所以我赢得的大查询甚至都没有执行,因为子查询返回多行

1 个答案:

答案 0 :(得分:0)

解决了这个问题。当我玩XML PATH时,我非常接近。以下是我最终的结果:

    SELECT      Customer.LastName + ', ' + Customer.FirstName AS 'Customer Name',
            Store.Name AS 'Store', 
            STUFF((SELECT '; ' + Item.ItemCode + ': ' + Item.Name
                   FROM  Item
                         INNER JOIN SaleItem ON SaleItem.ItemCode = Item.ItemCode
                   WHERE SaleItem.SaleID = Sale.SaleID
                   FOR XML PATH('')), 1, 1, '') [Item Code/Name]
            FROM    SaleItem
                    INNER JOIN Sale ON SaleItem.SaleID = Sale.SaleID
            WHERE   Sale.SaleID = SaleItem.SaleID
            GROUP BY Sale.SaleID, SaleItem.SaleID) AS 'Item', 
            Sale.SaleID AS 'Sale ID',
            Sale.SaleDate 'Date',
            Sale.SubTotal AS 'Subtotal', 
            Sale.GST AS 'GST'
            Sale.SaleTotal AS 'Sale Total'
FROM        Sale
            INNER JOIN Customer ON Sale.CustomerID = Customer.CustomerID
            INNER JOIN Store ON Sale.StoreID = Store.StoredID
            INNER JOIN SaleItem ON SaleItem.SaleNumber = Sale.SaleNumber
            INNER JOIN Item ON Item.ItemCode = SaleItem.ItemCode
WHERE       Sale.SaleDate >= DATEADD(MONTH, -1, GETDATE()) 
ORDER BY    Sale.SaleDate DESC

该查询正好显示了我想要的内容。感谢无论如何评论谁。