我有一个名为SurveyResponse的表,其中包含以下列
Id int, not null
SurveyId int, not null
QuestionId int, not null
Response varchar, null
QuestionPossibleAnswerId int, null
调查表包含以下列
Id int, not null
SurveyTypeId int, not null
CountyId int, not null
我们将可能的答案存储在表格中,以便调查动态创建。有些问题有一个文本框,他们可以输入一个响应,因为只有一些问题有文本框,响应字段可以为空。
我需要获得SurveyTypeId = 3的回复(3是当前年度调查) 并且Response不为null且QuestionId = 6(我需要问题6的评论)。
我遇到问题的部分是我只想要那些结果,其中QuestionId = 24,QuestionPossibleAnswerId = 111.问题24询问我们是否可以分享结果,而QuestionPossibleAnswerId 111的答案是肯定的。
select sr.Response,
(SELECT qpa1.PossibleAnswerText
FROM QuestionPossibleAnswer AS qpa1 INNER JOIN
SurveyResponse AS sr1 ON qpa1.Id = sr1.QuestionPossibleAnswerId
WHERE (sr1.QuestionId = 1) AND (sr1.SurveyId = sr.SurveyId)) AS County
FROM SurveyResponse AS sr
LEFT OUTER JOIN QuestionPossibleAnswer AS qpa ON sr.QuestionPossibleAnswerId = qpa.Id
INNER JOIN Survey AS s ON sr.SurveyId = s.Id
INNER JOIN SurveyType AS st ON s.SurveyTypeId = st.Id
WHERE (sr.Response IS NOT NULL) AND (sr.QuestionId = 6) AND (st.Id = 3)
ORDER BY County
这给了我问题6的响应和县名,但我需要过滤问题QuestionId = 24 AND QuestionPossibleAnswerId = 111
答案 0 :(得分:0)
使用存在的地方。
and exists(select 1 from SurveyResponse SR2 where QuestionId = 24 and
QuestionPossibleAnswerId = 111 and SR2.SurveyId = s.Id)
这将检查您正在考虑的调查是否有QuestionId = 24 AND QuestionPossibleAnswerId = 111.