group_concat () from MySql on SqlServer

4

I created a table for example:

I have the following content:

select * 
from table_t;

Youneedtogetthefollowingresult:
In MySql I use the following query:

select group_concat(concat_ws(' - ',id,descri)) id_descri, tipo 
from table_t
group by tipo;

What about SqlServer? Thanks!

    
asked by anonymous 27.04.2015 / 13:57

1 answer

2
SELECT tipo, STUFF((SELECT ', ' + CONVERT(VARCHAR, tipo) + ' - ' + descri
                    FROM table_t iT
                    WHERE iT.tipo = T.tipo
                    ORDER BY tipo
                    FOR XML PATH, TYPE).value('.[1]', 'NVARCHAR(max)'), 1, 2, '') AS id_descri
FROM table_t T
GROUP BY tipo

or more concise

SELECT tipo, STUFF((SELECT ', ' + CONVERT(VARCHAR, tipo) + ' - ' + descri
                    FROM table_t iT
                    WHERE iT.tipo = T.tipo
                    ORDER BY tipo
                    FOR XML PATH('')), 1, 2, '') AS id_descri
FROM table_t T
GROUP BY tipo
    
27.04.2015 / 14:59