oracle - SQL: Merging rows with comma separated values in columns -


we have table this:

group | user | team ------------------- grp1  | u1   | t1,t2 grp1  | u2   | t1,t2,t3 grp1  | u3   | t4 grp2  | u4   | t2,t4 grp2  | u5   | t5 

i want create view has data this:

group | teams ------------- grp1  | t1,t2,t3,t4 grp2  | t2,t4,t5 

can please me? tried doing few trail , errors , @ state not sure start now

here approach. using cursor can in while loop also. issue teams in new table not distinct think can play string commands , sort issue.

create table #tbl1 ([group] varchar(20), [user] varchar(20), team varchar(20))  insert #tbl1 ( [group], [user],team ) values  ( 'grp1',  'u1',  't1,t2'),   ( 'grp1',  'u2',  't3,t4'),   ( 'grp1',  'u3',  't4'),   ( 'grp2',  'u1',  't1,t2'),   ( 'grp2',  'u2',  't3,t4')  create table #tbl2 ([group] varchar(20), team varchar(20))  declare @listofteams varchar(max) declare @grp varchar(20) declare curs cursor fast_forward              select distinct [group]         #tbl1 t      open curs         fetch next curs @grp          while @@fetch_status = 0          begin              set @listofteams= ''             select @listofteams= @listofteams+ [team] + ',' #tbl1 cl cl.[group] = @grp order [cl].[user]              insert #tbl2   ( [group],team )             select distinct @grp,                          substring(@listofteams, 1, len(@listofteams)-1)             #tbl1 t             t.[group] = @grp;              fetch next curs @grp         end     close curs      deallocate curs;  select *  #tbl2 t 

here ow works: http://rextester.com/byk57259

+-------+----------------+ | group |      team      | +-------+----------------+ | grp1  | t1,t2,t3,t4,t4 | | grp2  | t1,t2,t3,t4    | +-------+----------------+ 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -