sql查询case语句多个条件
我有一个名为SurveyResponse的表,它有以下列sql查询case语句多个条件,sql,Sql,我有一个名为SurveyResponse的表,它有以下列 Id int, not null SurveyId int, not null QuestionId int, not null Response varchar, null QuestionPossibleAnswerId int, null
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
测量表包含以下列
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是本年度的调查
回答不是空的,问题ID=6,我需要问题6的评论
我遇到的问题是,我只想要那些结果,其中,对于问号id=24,问号possibleSwerid=111。问题24问我们是否可以分享结果和问题可能答案是肯定的
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的答案和县名,但我需要过滤case QuestionId=24和questionpossibleanswirid=111的地方,在存在的地方使用
and exists(select 1 from SurveyResponse SR2 where QuestionId = 24 and
QuestionPossibleAnswerId = 111 and SR2.SurveyId = s.Id)
这将检查您当前正在考虑的调查是否存在问题ID=24和问题可能性Swerid=111