Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql server Query question

Posted on 2013-01-04
4
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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