sql >> Base de Datos >  >> RDS >> Oracle

SQL Server equivalente de la función WM_CONCAT

No tiene una función equivalente para eso, pero aún puede simular (utilice CROSS APPLY y FOR XML PATH('') ). ejemplo,

USERID  ADDRESSLINE1
==========================
1       First Street
1       Second Street
2       32th Street
2       24th Street
2       25th Street

resultará

USERID  ADDRESSLIST
============================
1       First Street, Second Street
2       32th Street, 24th Street, 25th Street

Usando esta consulta:

SELECT  a.UserID, 
        SUBSTRING(d.Addresses,1, LEN(d.Addresses) - 1) AddressList
FROM
        (
            SELECT DISTINCT UserID
            FROM tableName
        ) a
        CROSS APPLY
        (
            SELECT [AddressLine1] + ', ' 
            FROM tableName AS B 
            WHERE A.UserID = B.UserID 
            FOR XML PATH('')
        ) D (Addresses) 

Demostración de SQLFiddle