sql server - Combining SQL records that have the same the same value in a given field -


let's say, example, have following table

courseid                             | coursetitle | entrymonth | entryyear | ebdef239-abb7-4a82-9229-1ed37496da86 | maths ft    | january    | 2013      | 57504a66-4882-4794-a8b9-af0ead38dc70 | maths ft    | february   | 2013      | f06c5e58-5563-4dfd-a8fc-2ce186c2106f | maths ft    | february   | 2014      | 0c81dfe6-0b11-4cad-a27c-970dbdb2876c | maths ft    | february   | 2015      | ebdef239-abb7-4a82-9229-1ed37496da86 | english pt  | january    | 2013      | 57504a66-4882-4794-a8b9-af0ead38dc70 | english pt  | january    | 2014      | 

is possible write query group coursetitle & entrymonth, combine entryyear values temporary column (preferably comma delimited). this:

courseid                             | coursetitle | entrymonth | newentryyear     | ebdef239-abb7-4a82-9229-1ed37496da86 | maths ft    | january    | 2013             | 57504a66-4882-4794-a8b9-af0ead38dc70 | maths ft    | february   | 2013, 2014, 2015 | ebdef239-abb7-4a82-9229-1ed37496da86 | english pt  | january    | 2013, 2014       | 

any examples appreciated. thanks.

select  coursetitle ,       entrymonth ,       stuff((         select  ', ' + cast(entryyear varchar)            table1 t2           t1.coursetitle = t2.coursetitle                 , t1.entrymonth = t2.entrymonth         xml path('')         ), 1, 2, '') years    table1 t1 group         coursetitle ,       entrymonth 

example @ sql fiddle.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -