Concatenate a single column into a comma delimited list
本问题已经有最佳答案,请猛点这里访问。
我看到过很多人将一个列汇总成逗号分隔的列表的例子,但我需要更多。
下面是一个我需要的数据和结果的例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | DECLARE @SalesPerson table (SalesPersonID int, SalesPersonName varchar(10)) DECLARE @Region table (RegionID int, RegionName varchar(15)) DECLARE @SalesPersonRegion table (SalesPersonID int, RegionID int) INSERT INTO @SalesPerson (SalesPersonID, SalesPersonName) VALUES (1,'Jeff') INSERT INTO @SalesPerson (SalesPersonID, SalesPersonName) VALUES (2,'Pat') INSERT INTO @SalesPerson (SalesPersonID, SalesPersonName) VALUES (3,'Joe') INSERT INTO @Region (RegionID, RegionName) VALUES (1,'North') INSERT INTO @Region (RegionID, RegionName) VALUES (2,'South') INSERT INTO @Region (RegionID, RegionName) VALUES (3,'East') INSERT INTO @Region (RegionID, RegionName) VALUES (4,'West') INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (1,1) INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (1,2) INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (1,3) INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (2,2) INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (2,3) INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (2,4) INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (3,1) INSERT INTO @SalesPersonRegion (SalesPersonID,RegionID) VALUES (3,4) |
一个简单的选择可以让我了解每个销售人员,以及每个销售人员所在的地区。
1 2 3 4 5 6 7 8 9 | SELECT sp.SalesPersonID, sp.SalesPersonName, r.RegionName FROM @SalesPersonRegion spr JOIN @SalesPerson sp ON spr.SalesPersonID = sp.SalesPersonID JOIN @Region r ON spr.RegionID = r.RegionID |
在这种情况下,它将返回9行。
我想得到这样的结果:
1 2 3 4 | SalesPersonID SalesPersonName Regions 1 Jeff North,South,East 2 Pat South,East,West 3 Joe North,West |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | SELECT sp.SalesPersonID, sp.SalesPersonName, Regions = STUFF ( ( SELECT ',' + r.RegionName FROM @Region AS r INNER JOIN @SalesPersonRegion AS spr ON r.RegionID = spr.RegionID WHERE spr.SalesPersonID = sp.SalesPersonID ORDER BY r.RegionID FOR XML PATH(''), TYPE ).value('.[1]','nvarchar(max)'), 1,1,'' ) FROM @SalesPerson AS sp ORDER BY sp.SalesPersonID; |
1 2 3 4 5 6 7 8 9 10 11 12 13 | select sp.SalesPersonID, sp.SalesPersonName, stuff( ( select ',' + r.RegionName from @SalesPersonRegion as spr inner join @Region as r on r.RegionID = spr.RegionID where spr.SalesPersonID = sp.SalesPersonID for xml path(''), type ).value('.', 'nvarchar(max)') , 1, 1, '') from @SalesPerson as sp |
SQL的例子湖小提琴
试试这个查询:
1 2 3 4 5 6 7 8 9 10 11 12 | SELECT sp.SalesPersonID, sp.SalesPersonName, reg.Regions FROM @SalesPerson sp CROSS APPLY( -- or OUTER APPLY SELECT STUFF( (SELECT ','+r.RegionName FROM @Region r INNER JOIN @SalesPersonRegion spr ON r.RegionID = spr.RegionID WHERE spr.SalesPersonID = sp.SalesPersonID FOR XML PATH('')),1,1,'') AS Regions )reg; |
结果:
1 2 3 4 5 | SalesPersonID SalesPersonName Regions ------------- --------------- ---------------- 1 Jeff North,South,East 2 Pat South,East,West 3 Joe North,West |