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
 General SQL Server Forums
 New to SQL Server Programming
 Get the matching record from different tables

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 Type
1 Single select
2 Multi Select
3 TextArea

QuestionTable
---------
QId QName InputID
1 Quality 1
2 Feedback abt
supervisor 2
3 Your comment 3


ChoiceMasterTable
---------
ChoiceID ChName
201 Good
202 Avg
203 Supportive
204 Approachable
205 Caring


QuestionOptionTable
----------------
QoptID QId ChoiceID
100 1 201
101 1 202
102 2 203
103 2 204



UserTable
-------
UserId UserName
600 Anu
601 Binu


Answertable
----------
AnsID UserID QoptID
1 600 101
2 600 102
3 600 103
4 601 100

=============
Expected result for user Anu


QID Question Answer

1 Quality avg
2 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 cases
Do a try and post if you face any issues. We will try to help then

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

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 Answer
From
sur_Answer ans inner join
sur_QuestionOptionTable qop on ans.QoptID = qop.QoptID
inner join QuestionTable qst on qop.QId = qst.QId
inner join ChoiceMasterTable och on qop.ChoiceID= och.ChoiceID
where
ans.UserId= 600;
---------------
The result got was below.
*****************
QID Question Answer

1 Quality avg
2 Supervisorfeedback Supportive
2 Supervisorfeedback Approachable

************************
Could you please tell if I can get the same question answer in one
column 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 cases
Do a try and post if you face any issues. We will try to help then

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-29 : 03:45:47
Use FOR XML PATH for CSV data.....

Refer this link
http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/

--
Chandu
Go to Top of Page

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 Answer
From
sur_Answer ans inner join
sur_QuestionOptionTable qop on ans.QoptID = qop.QoptID
inner join QuestionTable qst on qop.QId = qst.QId
inner join ChoiceMasterTable och on qop.ChoiceID= och.ChoiceID
where
ans.UserId= 600;
---------------
The result got was below.
*****************
QID Question Answer

1 Quality avg
2 Supervisorfeedback Supportive
2 Supervisorfeedback Approachable

************************
Could you please tell if I can get the same question answer in one
column 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 cases
Do a try and post if you face any issues. We will try to help then

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs





you can

like this


;With CTE
AS
(
Select
distinct qst.Name as Question,
qst.QuestionId,
och.OptionChoiceName as Answer
From
sur_Answer ans inner join
sur_QuestionOptionTable qop on ans.QoptID = qop.QoptID
inner join QuestionTable qst on qop.QId = qst.QId
inner join ChoiceMasterTable och on qop.ChoiceID= och.ChoiceID
where
ans.UserId= 600
)

SELECT c.*,
STUFF((SELECT ',' + Answer FROM CTE WHERE Question = c.Question AND QuestionId = c.QuestionId FOR XML PATH('')),1,1,'') AS Answer
FROM (SELECT DISTINCT Question,QuestionId FROM CTE)c


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -