合并SQL Server中的数据行

时间:2016-11-24 12:05:48

标签: sql-server consolidation

我有多行订单数据,我需要每个部分合并一行。

一个例子如下:

OrderNum      PartNum      Qty
-------------------------------    
1             24            2
2             25           10
3             24            5
4             24           10

然后需要合并到:

OrderNum      PartNum      Qty
-------------------------------    
1, 3, 4       24           17
2             25           10

有没有人有任何想法我怎么能这样做?

我在网上浏览过但无法找到这个用例的解决方案。

非常感谢,

5 个答案:

答案 0 :(得分:2)

试试这个

SELECT STUFF((SELECT ',' + CAST(OrderNum AS VARCHAR(4)) 
              FROM mytable AS s
              WHERE s.PartNum = t.PartNum
              FOR XML PATH('')), 1, 1, '') AS OrderNum
       PartNum, SUM(Qty)
FROM mytable AS t
GROUP BY PartNum

答案 1 :(得分:2)

这可以通过对PartNum进行分组,将数量与SUM()相加并使用相关子查询中的FOR XML PATH('')连接字符串来完成。在this answer on SO中解释了使用FOR XML PATH('')来连接字符串。

DECLARE @t TABLE(OrderNum INT, PartNum INT, Qty INT);
INSERT INTO @t(OrderNum,PartNum,Qty)
VALUES(1,24,2),(2,25,10),(3,24,5),(4,24,10);

SELECT
    OrderNum=STUFF((
        SELECT
            ','+CAST(i.OrderNum AS VARCHAR)
        FROM
            @t AS i
        WHERE
            i.PartNum=o.PartNum
        FOR XML
            PATH(''), TYPE
    ).value('.[1]','VARCHAR(MAX)'),1,1,''),
    o.PartNum,
    Qty=SUM(o.Qty)
FROM
    @t AS o
GROUP BY
    o.PartNum;

结果:

OrderNum | PartNum | Qty
------------------------
1,3,4    | 24      | 17
2        | 25      | 10

答案 2 :(得分:2)

DECLARE @t TABLE(OrderNum INT, PartNum INT, Qty INT)

INSERT INTO @t VALUES(1    ,         24   ,        2)
INSERT INTO @t VALUES(2    ,         25   ,        10)
INSERT INTO @t VALUES(3    ,         24   ,        5)
INSERT INTO @t VALUES(4    ,         24   ,        10)

SELECT OrderNum = 
    STUFF((SELECT ', ' + CONVERT(VARCHAR(50),OrderNum)
           FROM @t b 
           WHERE b.PartNum = a.PartNum 
          FOR XML PATH('')), 1, 2, ''),
          PartNum,
          SUM(Qty) as Qty
FROM @t a
GROUP BY PartNum

结果

enter image description here

答案 3 :(得分:2)

SQL Server 2016添加了STRING_AGG功能。

在你的情况下,你可以写

select STRING_ACC(OrderId,','),PartNum, Sum(Qty)
from MyTable
Group by PartNum

对于早期版本,您必须使用Aaron Bertrand在Grouped Concatenation in SQL Server中描述的技术之一。最快的是使用SQLCLR方法。接下来是@GiorgosBetsos发布的FOR XML方法

答案 4 :(得分:1)

有很多方法可以做到这一点。

create table tablename (Name varchar(100), Rnk int)
Insert into tablename values
('Northshore', 1),
('F3', 2),
('Borderline', 3),
('Mattoon',3),
('Vinemane',5),
('Arizona',5),
('WestShore', 5),
('Schumburg', 5),
('Wilson',5)

--Method2
Select distinct 
   names=  REPLACE( 
                ( 
          Select a.Name as [data()]
       From tablename A
    Where A.Rnk = b.Rnk
    Order by a.Name
    FOR XML PATH ('') ), ' ', ',') ,Rnk

    From tablename B Order by Rnk

OR

CREATE TABLE TestTable (ID INT, Col VARCHAR(4))
GO
INSERT INTO TestTable (ID, Col)
SELECT 1, 'A'
UNION ALL
SELECT 1, 'B'
UNION ALL
SELECT 1, 'C'
UNION ALL
SELECT 2, 'A'
UNION ALL
SELECT 2, 'B'
UNION ALL
SELECT 2, 'C'
UNION ALL
SELECT 2, 'D'
UNION ALL
SELECT 2, 'E'
GO
SELECT *
FROM TestTable
GO


-- Get CSV values
SELECT t.ID, STUFF(
(SELECT ',' + s.Col
FROM TestTable s
WHERE s.ID = t.ID
FOR XML PATH('')),1,1,'') AS CSV
FROM TestTable AS t
GROUP BY t.ID
GO

OR

CREATE  TABLE  #mable(mid INT, token nvarchar(16))

INSERT INTO #mable VALUES (0, 'foo')
INSERT INTO #mable VALUES(0, 'goo')
INSERT INTO #mable VALUES(1, 'hoo')
INSERT INTO #mable VALUES(1, 'moo')


SELECT m1.mid,
       ( SELECT m2.token + ','
           FROM #mable m2
          WHERE m2.mid = m1.mid
          ORDER BY token
            FOR XML PATH('') ) AS token
  FROM #mable m1
 GROUP BY m1.mid ;

另外,请看这个。

http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/