[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL 2008 Query to produce a report

Posted on 2015-01-30
11
Medium Priority
?
120 Views
Last Modified: 2015-01-30
Dear Experts,

This should be simple, but I’m having a brain dead day.  A day where you leave your coffee on top of the car and drive off.  I want to produce a query where the results show on each row the StudId in one column and his Scores in the second columns separated by a comma.

StudId   Scores
1            81,92,99,65,78,56
2            83,89,43,72,65,77
35         98,96,83

I have attached code to create the test table and insert the raw data.
INSERT-INTO-Scores.sql
CREATE-SCORES.sql
0
Comment
Question by:wdbates
[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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40580057
Can I just say "thank you". So few people actually give us a sample data and sample structure. So:

Thank you
Thank you
Thank you
Thank you
Thank you
Thank you

I'm be back in a minute.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 40580071
SELECT [StudId], SUBSTRING(
 (SELECT ',' + cast (s.[Score] AS VARCHAR(20) )
FROM dbo.Scores s
WHERE s.studID = o.studid
ORDER BY s.[Score]
FOR XML PATH('')),2,200000) AS CSV
FROM dbo.Scores o
GROUP by studID
 GO
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 1000 total points
ID: 40580072
select StudID, 
STUFF((select ', ' + CONVERT(VARCHAR(10),[Score]) from dbo.Scores as T
where T.StudID = S.StudID
FOR XML PATH ('')),1,3,'') AS Scores
FROM dbo.Scores as S
group by StudID

Open in new window

0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40580073
Ahhhh! 6 seconds!
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40580087
@Philip, I waited for 5minutes before I posted my solution, I thought you became busy  after all we all have full time jobs..
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 40580091
Phillip lost time thanking for the samples :)
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40580101
It's true!
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40580116
But it was worth it. How much easier does it make your life?
0
 

Author Closing Comment

by:wdbates
ID: 40580128
You guys are great!  I'm going half and half.  Aneesh, you were first, but Phillip through in STUFF()  which is something new for me.  I always like to learn something new.
0
 

Author Comment

by:wdbates
ID: 40580439
Phillip,  Just noticed that your answer was not correct.  Where did the score of 1 come from StudId 1, and 3 from StudId 2 and the score of 8 from StudId35?  Let me know what you discover.  Thanks!

StudID      Scores
1             1, 92, 99, 65, 78, 56
2             3, 89, 43, 72, 65, 77
35             8, 96, 83

Correct answer:
StudId      CSV
1             56,65,78,81,92,99
2             43,65,72,77,83,89
35             83,96,98
0
 

Author Comment

by:wdbates
ID: 40580810
Phillip, Had a chance to go back and look at your query and changed the STUFF value ,1,3,'' to 1,2,''.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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