1

我有三张桌子。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           |    

第一个是数字,问题,然后是所有可能的答案。第二个只是数字,问题,只是选择的答案

4

2 回答 2

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 不在响应表中。

于 2013-02-02T19:01:38.933 回答
0

添加一个条件,即响应必须针对正确的问题:

inner join Responses r
    on r.ResponseCode = sf.ResponseCode
       and r.QuestionID = q.QuestionID
于 2013-02-02T18:29:06.533 回答