Reputation: 689
I have three tables. SurveyFact, Question, and Responses. The survey fact contains the data for surveys taken by clients while the Question and Responses table has the obvious data, a list of questions and possible responses per Question.
SurveyFact:
| SurveyFactID | ClientID | QuestionID | ResponseCode |
------------------------------------------------------------
| 1 | 1 | 1 | 3 |
| 2 | 1 | 2 | 3 |
| 3 | 1 | 3 | 1 |
Question:
| QuestionID| QuestionText |
-------------------------------------
| 1 | blah blah blah |
| 2 | blah blah blah |
| 3 | blah blah blah |
Response:
| ResponseID| QuestionID | ResponseCode |ResponseText |
-----------------------------------------------------------|
| 1 | 1 | 1 | like |
| 2 | 1 | 2 | don't care |
| 3 | 1 | 3 | hate |
| 4 | 2 | 1 | like |
| 5 | 2 | 2 | don't care |
| 6 | 2 | 3 | hate |
Here is the query I've come up with. (That fails)
select
sf.QuestionCode as [Question Number],
q.QuestionText as [Question Text],
r.ResponseText as [Reponse]
from SurveyFact sf
inner join Question q
on q.QuestionID = sf.QuestionID
inner join Responses r
on r.ResponseCode = sf.ResponseCode
where sf.ClientID = '1'
and sf.QuestionID = q.QuestionID
and sf.ResponseCode = r.ResponseCode
Even when I use select distinct it yields me the survey and questions with every possible answer instead just the the question/answer combo listed in SurveyFact.
Help please?
Requested Table: What I'm Seeing
| Question Number | Question Text |Response Text |
------------------------------------------------------
| 1 | blah blah blah | like |
| 1 | blah blah blah | don't care |
| 1 | blah blah blah | hate |
| 2 | blah blah blah | like |
| 2 | blah blah blah | don't care |
| 2 | blah blah blah | hate |
What I want:
| Question Number | Question Text |Response Text |
------------------------------------------------------
| 1 | blah blah blah | don't care |
| 2 | blah blah blah | like |
First one is Number, QUestion, Then every possible answer. Second is just Number,Question, just chosen answer
Upvotes: 1
Views: 92
Reputation: 8597
select
sf.QuestionID as QuestionNumber,
q.QuestionText as QuestionText,
r.ResponseText as Reponse
from SurveyFact sf, Question q, Response r
where sf.ClientID = '1'
and
sf.QuestionID = q.QuestionID
and
r.QuestionID = q.QuestionID
and
sf.ResponseCode = r.ResponseCode
I think is what you are looking for.
The result will be:
| Question Number | Question Text |Response Text |
------------------------------------------------------
| 1 | blah blah (q1) | hate |
| 2 | blah blah (q2) | hate |
In your result you wrote:
| Question Number | Question Text |Response Text |
------------------------------------------------------
| 1 | blah blah blah | don't care |
| 2 | blah blah blah | like |
But I can not see how "dont care" and "like" should be fetched? They have ResponseCode 1 and 2. And ResultCode 1 and 2 are not in the SurveyFact. Well 1 is, but it is applied for question 3, and question 3 is not in the Response-table.
Upvotes: 1
Reputation: 238086
Add a condition that the response must be for the proper question:
inner join Responses r
on r.ResponseCode = sf.ResponseCode
and r.QuestionID = q.QuestionID
Upvotes: 0