我有三张桌子。SurveyFact、问题和响应。调查事实包含客户进行的调查的数据,而问题和响应表包含明显的数据、问题列表和每个问题的可能响应。
调查事实:
| SurveyFactID | ClientID | QuestionID | ResponseCode |
------------------------------------------------------------
| 1 | 1 | 1 | 3 |
| 2 | 1 | 2 | 3 |
| 3 | 1 | 3 | 1 |
问题:
| QuestionID| QuestionText |
-------------------------------------
| 1 | blah blah blah |
| 2 | blah blah blah |
| 3 | blah blah blah |
回复:
| 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 |
这是我提出的查询。(那失败了)
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
即使当我使用 select distinct 时,它也会为我生成带有每个可能答案的调查和问题,而不是仅在 SurveyFact 中列出的问题/答案组合。
请帮忙?
要求的表:我所看到的
| 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 |
我想要的是:
| Question Number | Question Text |Response Text |
------------------------------------------------------
| 1 | blah blah blah | don't care |
| 2 | blah blah blah | like |
第一个是数字,问题,然后是所有可能的答案。第二个只是数字,问题,只是选择的答案