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.
Author |
Topic |
chill23
Starting Member
1 Post |
Posted - 2014-06-17 : 14:40:49
|
I'd like to create a report to display all the answers to a form on one line with each question as a separate column. Here is the table:ID Form_ID QuestionID AnswerValue1 1 123 Paul2 1 124 Smith3 1 125 184 2 123 Mary5 2 124 Samsonite6 2 125 24Here is what I'd like it to beForm_ID Firstname Surname Age1 Paul Smith 182 Mary Samsonite 24Please note how I have changed the questionid tag to be more meaningful. Any help on this will be much appreciated.Thank you. |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2014-06-17 : 14:44:39
|
You can use a Pivot or a Aggregate function with a CASE expression. Here is a quick example of how to use a CASE expression:SELECT MAX(CASE WHEN QuestionID = 123 THEN AnswerValue ELSE NULL END) AS FirstName ,MAX(CASE WHEN QuestionID = 124 THEN AnswerValue ELSE NULL END) AS LastName ,...FROM <TableName>GROUP BY Form_ID |
|
|
|
|
|