如何只从唯一键填充值

时间:2016-11-10 20:22:43

标签: sql sql-server sql-server-2008 tsql

我认为这是一个非常简单的问题,答案同样简单,但我无法弄明白。

我有一个像这样建模的临时表,查询返回如下数据:

+--------+------+---------+---------+--------+-----------+--------+-----+
| Acct # | PO # | Store # | Order # | Line # | Ship Date | Item # | Qty |
+--------+------+---------+---------+--------+-----------+--------+-----+
|  0777  | 3340 |   648   | 1756013 |   1    | 11/23/16  | 100598 |  1  |
|  0777  | 3340 |   648   | 1756013 |   2    | 11/23/16  | 100597 |  1  |
|  0777  | 3340 |   648   | 1756013 |   3    | 11/23/16  | 100598 |  1  |
|  0777  | 3340 |   648   | 1756013 |   4    | 11/23/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 |   1    | 12/01/16  | 150962 |  1  |
|  0777  | 3341 |   720   | 1760001 |   3    | 12/01/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 |   4    | 12/01/16  | 150961 |  1  |
|  0777  | 3341 |   720   | 1760001 |   7    | 12/01/16  | 150961 |  1  |
|  0777  | 3341 |   720   | 1760001 |   8    | 12/01/16  | 156961 |  1  |
+--------+------+---------+---------+--------+-----------+--------+-----+

我应该将类似行合并在一起,以便数量从1增加到2等,对于订单号上相同的每个项目编号。我有这个部分工作,但为了做到这一点,我不得不摆脱行号,所以返回的数据看起来像这样:

+--------+------+---------+---------+-----------+--------+-----+
| Acct # | PO # | Store # | Order # | Ship Date | Item # | Qty |
+--------+------+---------+---------+-----------+--------+-----+
|  0777  | 3340 |   648   | 1756013 | 11/23/16  | 100598 |  2  |
|  0777  | 3340 |   648   | 1756013 | 11/23/16  | 100597 |  1  |
|  0777  | 3340 |   648   | 1756013 | 11/23/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 | 12/01/16  | 150962 |  1  |
|  0777  | 3341 |   720   | 1760001 | 12/01/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 | 12/01/16  | 150961 |  3  |
+--------+------+---------+---------+-----------+--------+-----+

但是,我需要Line#列来准确报告正在处理,发货的订单行等。我知道STUFF()函数可以执行此操作,只是它添加了每一行#而不是只是订单#特有的。这是我的疑问:

SELECT DISTINCT
    SupplierAcctNumber AS 'Acct #',
    BuyerPONumber AS 'PO #',
    BuyerStoreNumber AS 'Store #',
    SupplierOrderNumber AS 'Order #',
    SupplierOrderLine =
        STUFF((SELECT ',' + CAST(t.SupplierOrderLine AS VARCHAR)
            FROM @temp t
            WHERE t.SupplierOrderNumber = SupplierOrderNumber
                AND t.BuyerItemNumber = BuyerItemNumber FOR XML PATH ('')),1,1,''),
    SupplierShipDate AS 'Ship Date',
    BuyerItemNumber AS 'Item #',
    SUM(SupplierQtyOrdered) AS 'Qty'
FROM @temp
GROUP BY SupplierAcctNumber, BuyerPONumber, BuyerStoreNumber, SupplierOrderNumber,
    SupplierOrderLine, SupplierShipDate, BuyerItemNumber
ORDER BY SupplierOrderNumber

以下是我得到的结果:

+--------+------+---------+---------+-------------------+-----------+--------+-----+
| Acct # | PO # | Store # | Order # | Line #            | Ship Date | Item # | Qty |
+--------+------+---------+---------+-------------------+-----------+--------+-----+
|  0777  | 3340 |   648   | 1756013 | 1,2,3,4,1,3,4,7,8 | 11/23/16  | 100598 |  2  |
|  0777  | 3340 |   648   | 1756013 | 1,2,3,4,1,3,4,7,8 | 11/23/16  | 100597 |  1  |
|  0777  | 3340 |   648   | 1756013 | 1,2,3,4,1,3,4,7,8 | 11/23/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 | 1,2,3,4,1,3,4,7,8 | 12/01/16  | 150962 |  1  |
|  0777  | 3341 |   720   | 1760001 | 1,2,3,4,1,3,4,7,8 | 12/01/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 | 1,2,3,4,1,3,4,7,8 | 12/01/16  | 150961 |  3  |
+--------+------+---------+---------+-------------------+-----------+--------+-----+

以下是我预期的结果:

+--------+------+---------+---------+-------------------+-----------+--------+-----+
| Acct # | PO # | Store # | Order # | Line #            | Ship Date | Item # | Qty |
+--------+------+---------+---------+-------------------+-----------+--------+-----+
|  0777  | 3340 |   648   | 1756013 | 1,3               | 11/23/16  | 100598 |  2  |
|  0777  | 3340 |   648   | 1756013 | 2                 | 11/23/16  | 100597 |  1  |
|  0777  | 3340 |   648   | 1756013 | 4                 | 11/23/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 | 1                 | 12/01/16  | 150962 |  1  |
|  0777  | 3341 |   720   | 1760001 | 3                 | 12/01/16  | 100596 |  1  |
|  0777  | 3341 |   720   | 1760001 | 4,7,8             | 12/01/16  | 150961 |  3  |
+--------+------+---------+---------+-------------------+-----------+--------+-----+

2 个答案:

答案 0 :(得分:0)

这是一种方式。我相信还有更好的......但是对于你的结构它会起作用。 另外,认为150961应该是你最后一条记录中的项目#而不是156961

select
    [Acct #],
    [PO #],
    [Store #],
    [Order #],
    STUFF((SELECT ', ' + cast([Line #] as varchar) from #tt where [Order #] = t.[Order #] and [Item #] = t.[Item #] FOR XML PATH ('')), 1, 1, '') as [Line #],
    [Ship Date],
    [Item #],
    SUM(Qty) as Qty
from #tt t
group by 
    [Acct #],
    [PO #],
    [Store #],
    [Order #],
    [Ship Date],
    [Item #]

答案 1 :(得分:0)

正如评论中所提到的,这个问题与STUFF的编写方式简单混合。调整后,查询应该像这样,在stuff语句之外使用@temp的表别名:

SELECT DISTINCT
    SupplierAcctNumber AS 'Acct #',
    BuyerPONumber AS 'PO #',
    BuyerStoreNumber AS 'Store #',
    SupplierOrderNumber AS 'Order #',
    SupplierOrderLine =
        STUFF((SELECT ',' + CAST(t.SupplierOrderLine AS VARCHAR)
            FROM @temp
            WHERE t.SupplierOrderNumber = SupplierOrderNumber
                AND t.BuyerItemNumber = BuyerItemNumber FOR XML PATH ('')),1,1,''),
    SupplierShipDate AS 'Ship Date',
    BuyerItemNumber AS 'Item #',
    SUM(SupplierQtyOrdered) AS 'Qty'
FROM @temp t
GROUP BY SupplierAcctNumber, BuyerPONumber, BuyerStoreNumber, SupplierOrderNumber,
    SupplierOrderLine, SupplierShipDate, BuyerItemNumber
ORDER BY SupplierOrderNumber;