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 |
razeena
Yak Posting Veteran
54 Posts |
Posted - 2013-05-28 : 15:35:53
|
Hi, I have 5 tables related to a user answering the questionpaper.The aim is to display the question and its answer.If the question is of multiselect,then answer is shown as comma separated.Please see the sample table and expected result below.InputCategory-------InputID Type1 Single select2 Multi Select3 TextAreaQuestionTable---------QId QName InputID1 Quality 12 Feedback abt supervisor 23 Your comment 3ChoiceMasterTable---------ChoiceID ChName 201 Good202 Avg203 Supportive204 Approachable205 CaringQuestionOptionTable----------------QoptID QId ChoiceID100 1 201101 1 202102 2 203103 2 204UserTable-------UserId UserName600 Anu601 BinuAnswertable----------AnsID UserID QoptID 1 600 1012 600 1023 600 1034 601 100=============Expected result for user AnuQID Question Answer1 Quality avg2 Supervisorfeedback Supportive,Approachable |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-29 : 01:06:14
|
Can we see what you tried so far?Its a straightforward join between tables that you need. Also there need to be logic to concatenating the matching values for multi select casesDo a try and post if you face any issues. We will try to help then------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
razeena
Yak Posting Veteran
54 Posts |
Posted - 2013-05-29 : 03:39:03
|
Apologies.I should have posted the code I have tried.----Select distinct qst.Name as Question, qst.QuestionId, och.OptionChoiceName as AnswerFromsur_Answer ans inner joinsur_QuestionOptionTable qop on ans.QoptID = qop.QoptIDinner join QuestionTable qst on qop.QId = qst.QIdinner join ChoiceMasterTable och on qop.ChoiceID= och.ChoiceIDwhereans.UserId= 600;---------------The result got was below.*****************QID Question Answer1 Quality avg2 Supervisorfeedback Supportive2 Supervisorfeedback Approachable ************************Could you please tell if I can get the same question answer in onecolumn with comma separated like this.2 Supervisorfeedback Supportive,Approachable quote: Originally posted by visakh16 Can we see what you tried so far?Its a straightforward join between tables that you need. Also there need to be logic to concatenating the matching values for multi select casesDo a try and post if you face any issues. We will try to help then------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-29 : 03:53:22
|
quote: Originally posted by razeena Apologies.I should have posted the code I have tried.----Select distinct qst.Name as Question, qst.QuestionId, och.OptionChoiceName as AnswerFromsur_Answer ans inner joinsur_QuestionOptionTable qop on ans.QoptID = qop.QoptIDinner join QuestionTable qst on qop.QId = qst.QIdinner join ChoiceMasterTable och on qop.ChoiceID= och.ChoiceIDwhereans.UserId= 600;---------------The result got was below.*****************QID Question Answer1 Quality avg2 Supervisorfeedback Supportive2 Supervisorfeedback Approachable ************************Could you please tell if I can get the same question answer in onecolumn with comma separated like this.2 Supervisorfeedback Supportive,Approachable quote: Originally posted by visakh16 Can we see what you tried so far?Its a straightforward join between tables that you need. Also there need to be logic to concatenating the matching values for multi select casesDo a try and post if you face any issues. We will try to help then------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
you canlike this;With CTEAS(Select distinct qst.Name as Question, qst.QuestionId, och.OptionChoiceName as AnswerFromsur_Answer ans inner joinsur_QuestionOptionTable qop on ans.QoptID = qop.QoptIDinner join QuestionTable qst on qop.QId = qst.QIdinner join ChoiceMasterTable och on qop.ChoiceID= och.ChoiceIDwhereans.UserId= 600)SELECT c.*,STUFF((SELECT ',' + Answer FROM CTE WHERE Question = c.Question AND QuestionId = c.QuestionId FOR XML PATH('')),1,1,'') AS AnswerFROM (SELECT DISTINCT Question,QuestionId FROM CTE)c ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|