Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL SQL-Server
Hi,
 
Here's a snapshot of the data I have
 
SubjectID       StudentName
----------      -------------
1               Mary
1               John
1               Sam
2               Alaina
2               Edward
 
I need to group it as below:
 
SubjectID       StudentName
----------      -------------
1               Mary, John, Sam
2               Alaina, Edward
 
Any solutions based on COALESCE? Co-Related Sub Query? or probably even simpler ways?
Posted 6-Feb-12 22:36pm
Edited 6-Feb-12 22:42pm
v2
Comments
walterhevedeich at 7-Feb-12 4:43am
   
What have you tried?
Jyothikarthik_N at 7-Feb-12 4:46am
   
I've tried COALESCE:
 
DECLARE @StudName As VARCHAR(MAX)
SELECT @StudName=COALESCE(@StudName+ StudentName) FROM table WHERE
SELECT @StudName
 
This query simply gives me:
Mary, John, Sam, Alaina, Edward
 
I'm getting hit at a way to combine the SubjectID with the StudentName.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There's a tip ^] that could do the trick for you.
  Permalink  
Comments
Jyothikarthik_N at 7-Feb-12 7:20am
   
That should solve, thanks!
Jörgen Andersson at 7-Feb-12 7:28am
   
You're welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,205
1 OriginalGriff 6,801
2 CPallini 5,350
3 George Jonsson 3,644
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 7 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100