sql - Rolling up multiple rows into single row using 2 tables -
is there way change sql query return multiple rows same values single row comma separated?
table1 ------ col1 ------ sci-fi action crime table2 ------------ col1 | col2 ------------ 1 | action 1 | sci-fi 2 | crime 2 | action 2 | sci-fi
and need query results this: (table1 , table2 combined)
---------------------------- col1 | col2 ---------------------------- 1 | action, sci-fi 2 | crime, action, sci-fi
select mg.movie_id , stuff(( select ',' + g.genre_name movie_genre g g.movie_id = mg.movie_id order g.genre_name xml path('') ), 1, 1, '') genres movie_genre mg group mg.movie_id
credit this post crazy awesome stuff expression.
Comments
Post a Comment