Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Summarize Data for Pivot

Author  Topic 

richxyz
Starting Member

12 Posts

Posted - 2007-08-30 : 15:46:02
I have a table that I need to pivot to make the data more readable for the user. The problem, you will see, is that Question3 has multiple answers (zero to 5 possible answers.)

Here is the data of the source

UserID QuestionID Answer
1 1 B1
1 2 B2
1 3 A
1 3 B
1 3 C
2 1 ZZ
2 2 abc
2 3 1a
2 3 1x

The output needs to look like this

UserID Question1 Question2 Question3
1 B1 B2 A, B, C
2 ZZ abc 1a, 1x

I have an ugly cursor that does this and it is taking 5 hours to run. I'm wondering if anyone has a slicker way?


jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-08-30 : 15:49:52
Where are you displaying this data to the user? You should simply return data from SQL with its usual multi-row output and format your data at your presentation layer however you'd like. SQL Server is a not a formatting tool, it is a database.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

richxyz
Starting Member

12 Posts

Posted - 2007-08-30 : 16:01:15
I am copying and pasting the data into Excel. I will still have the problem (in Excel) with question 3. I don't know how I'd concantonate the multiple values into one cell. I can figure out how to do the pivot table - but can you offer a suggestion on collapsing the multiple answers of Question3 into one?
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-08-30 : 16:30:43
Ah, I see.

Some links that will help you:

http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx
http://omnibuzz-sql.blogspot.com/2006/06/concatenate-values-in-column-in-sql.html


- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

richxyz
Starting Member

12 Posts

Posted - 2007-08-30 : 17:31:45
Thank you. This is very helpful!
Go to Top of Page
   

- Advertisement -