2013-02-02 46 views
1

我有三个表。 SurveyFact,问题和答复。调查事实包含客户进行调查的数据,而问题和响应表包含明显的数据,问题列表和每个问题可能的答案。无法形成SQL查询

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 

即使当我使用不同的选择它产生了我的调查和问题与每一个可能的答案,而不是仅仅的问题/答案组合在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   |  

第一个是数量,问题,那么每一个可能的答案。二是刚刚号码,问,只是选择了答案

+0

你能否描述一下你实际期望的结果?制作一个选择查询的表格,然后帮助你更容易。 – MrSimpleMind

+0

'在q.QuestionID = sf.QuestionID'你在SurveyFact表中没有字段QuestionID,所以这个查询可能会产生一个错误...错字或缺少的东西? –

+0

添加结果表(实际和想要的)和调查问题中的questionCode是QuestionID。修正它以反映这一点。 –

回答

1
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 

我想是你在找什么。

结果将是:

| Question Number | Question Text |Response Text  | 
------------------------------------------------------ 
| 1  | blah blah (q1) | hate   | 
| 2  | blah blah (q2) | hate   | 

在您的结果中写道:

| Question Number | Question Text |Response Text  | 
------------------------------------------------------ 
| 1  | blah blah blah | don't care  | 
| 2  | blah blah blah | like   | 

,但我看不出“不关心”和“喜欢”应该是牵强? 它们具有ResponseCode 1和2. 并且ResultCode 1和2不在SurveyFact中。 好的1是,但它适用于问题3,问题3不在响应表中。

+0

要回答有关返回结果的问题,我没有真正执行查询,我只是说明我只想每个问题得到一个结果,而不是每个问题的所有结果。而你的回应是我解决这个问题的方法,所以谢谢:) –

0

添加一个条件,即反应必须是正确的问题:

inner join Responses r 
    on r.ResponseCode = sf.ResponseCode 
     and r.QuestionID = q.QuestionID