Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql server Query question

Posted on 2013-01-04
4
Medium Priority
?
288 Views
Last Modified: 2013-01-04
Hi,

I have a student table & has three coulumns. The sample data exists in this table as below.
I want a query which should output(please see in expected output). I don't want to use cursor.

Student table with sample row

StudenRowid  Name   course
1                       paul      Math
1                       paul       English
2                       David     science
2                       David     English
3                       Tim        Matk

Expected output:

Name    course
Paul          Math,English
David         Science,English
Tim          Math


Thanks for your help.
0
Comment
Question by:r_pat72
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 38745531
Try this:
select DISTINCT Name
, REPLACE((select Course AS [data()] from #students s where s.SudentRowId = x.SudentRowId for xml path('')), ' ', ',') AS Course_List
from students x

Open in new window

0
 

Author Comment

by:r_pat72
ID: 38745628
Hi ,

Thanks for your reply. But if the course is social science, the results which returns is social,science.

StudenRowid  Name   course
1                       paul      Math
1                       paul       English
2                       David     science
2                       David     English
3                       Tim        Math
4                        Tom     Social science

For Tom it returns social,science which should return social science.

Thanks
0
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 2000 total points
ID: 38745660
Ah, the embedded space, then this is another way to do it and should work.  I just liked the elegance of the REPLACE instead of SUBSTRING
select DISTINCT Name
, substring((select ','+Course from students s where s.SudentRowId = x.SudentRowId for xml path('')),2,2000) as Couse_list
from students x

Open in new window

Just make sure you have the last value for the length in SUBSTRING long enough.

Sample output:
David	Science,English
paul	Math,English
Tim	Social Science

Open in new window

0
 

Author Comment

by:r_pat72
ID: 38745697
thank a lot. This worked.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question