Solved

sql server Query question

Posted on 2013-01-04
4
277 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 26

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 26

Accepted Solution

by:
Chris Luttrell earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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