5

1 つの列をカンマ区切りのリストにまとめる例を複数見てきましたが、もう少し詳しく説明する必要があります。

必要なデータと結果の例を次に示します。

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)

選択するだけで、各営業担当者と、その担当者の地域ごとに取得できます。

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 行が返されます。

次のような結果を得たいと思います。

SalesPersonID    SalesPersonName      Regions
1                Jeff                 North,South,East
2                Pat                  South,East,West
3                Joe                  North,West
4

3 に答える 3

9
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;
于 2013-08-09T20:48:37.240 に答える
2

このクエリを試してください:

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;

結果:

SalesPersonID SalesPersonName Regions
------------- --------------- ----------------
1             Jeff            North,South,East
2             Pat             South,East,West
3             Joe             North,West
于 2013-08-09T20:48:50.350 に答える
2
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フィドルの例を参照してください

于 2013-08-09T20:49:13.240 に答える