views:

50

answers:

1

I've got a table Employees with employees (say the important fields are ID int, Name varchar(50)) and a table Areas with sales areas (ID int, EmployeeID int, USState char(2)).

The sample values are:

Employees
ID     Name
1      Shoeman
2      Smith
3      Johnson

Areas
ID     EmployeeID     USState
1      1              NY
2      1              FL
3      1              AR
4      2              DC
5      2              AR
6      3              TX

Can anyone give me a hint on making a SQL query to get the output recordset in the following way:

EmployeeID     USState
1              NY FL AR
2              DC AR
3              TX

Target platform: SQL Server 2005.

+2  A: 

This operation is called GROUP_CONCAT in MySQL but SQL Server doesn't support it.

In SQL Server you can simulate the same functionality using the FOR XML PATH hack.

SELECT extern.EmployeeID, states AS USState
FROM Areas AS extern
CROSS APPLY (
    SELECT USState + ' '
    FROM Areas AS intern
    WHERE extern.EmployeeID = intern.EmployeeID
    FOR XML PATH('')
) T1 (states)
GROUP BY extern.EmployeeID, states
ORDER BY extern.EmployeeID

An alternative is to use a recursive CTE. This is a less hacky solution but it is also more complicated:

WITH qs(EmployeeID, USState, rn, cnt) AS
(
    SELECT
        EmployeeID,
        USState,
        ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY USState),
        COUNT(*) OVER (PARTITION BY EmployeeID)
    FROM    Areas
),
t (EmployeeID, prodname, gc, rn, cnt) AS
(
    SELECT EmployeeID, USState, CAST(USState AS NVARCHAR(MAX)), rn, cnt
    FROM qs
    WHERE rn = 1
    UNION ALL
    SELECT
        qs.EmployeeID, qs.USState,
        CAST(t.gc + ' ' + qs.USState AS NVARCHAR(MAX)),
        qs.rn, qs.cnt
    FROM t
    JOIN qs ON qs.EmployeeID = t.EmployeeID
           AND qs.rn = t.rn + 1
)
SELECT EmployeeID, gc AS USState
FROM   t
WHERE  rn = cnt
ORDER BY EmployeeID
OPTION (MAXRECURSION 0)

Both methods give the result you want:

EmployeeID     USState
1              NY FL AR 
2              DC AR 
3              TX 
Mark Byers
@Mark your 'hack' code did the job! Thanks for your help
Viaceslav Dymarski
I like CTE, but till now no time used recursive CTE. Thanks for an interesting example of this!
Oleg