1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| WITH CTE(a, liste, b, lgr) AS
(
SELECT a,
CAST('' AS VARCHAR(MAX)) liste,
CAST('' AS VARCHAR(MAX)) b,
0 lgr
FROM dbo.TOTO
GROUP BY a
UNION ALL
SELECT TOTO.a a,
CAST(liste + CASE WHEN lgr = 0 THEN '' ELSE ', ' END + TOTO.b AS VARCHAR(MAX)) liste,
CAST(TOTO.b AS VARCHAR(MAX)) b,
lgr + 1 lgr
FROM CTE
JOIN dbo.TOTO TOTO ON CTE.a = TOTO.a
WHERE TOTO.b > CTE.b
)
SELECT a, liste
FROM (
SELECT a, liste, RANK() OVER (PARTITION BY a ORDER BY lgr DESC) rang
FROM CTE
) D
WHERE rang = 1 |
Partager