合并 SQL 服务器中的数据行
Consolidate rows of data in SQL Server
我有多行订单数据,我需要将每个部分合并为一行。
举例如下:
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
有人知道我该怎么做吗?
我在网上四处看了看,但找不到这个用例的解决方案。
非常感谢,
试试这个
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
这可以通过对 PartNum 进行分组,用 SUM()
对数量求和并在相关子查询中使用 FOR XML PATH('')
连接字符串来完成。 .
中解释了使用 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
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
结果
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 方法
有很多方法可以做到这一点。
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
或
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
或
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/
我有多行订单数据,我需要将每个部分合并为一行。
举例如下:
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
有人知道我该怎么做吗?
我在网上四处看了看,但找不到这个用例的解决方案。
非常感谢,
试试这个
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
这可以通过对 PartNum 进行分组,用 SUM()
对数量求和并在相关子查询中使用 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
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
结果
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 方法有很多方法可以做到这一点。
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
或
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
或
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/