我会提前说我知道这是一个混乱的查询。数据库不是由我管理的,因此某些关系是长期而复杂的。这是一个托管的EMR,因此对于可以使用的SQL命令,我的访问量非常少。例如,在第一个SELECT语句之前,我什么都没有。再有一个事实是,SQL只是我了解的一小部分,因此这些报告被推向我,而我不是SQL专家。
那表示我正尝试提出3个单独的查询,以回答我们在表单上遇到的问题。我不确定如何用正确的连接来绑定它们,并且毫无疑问,有更好的方法来实现它。
这是我现有的查询:
Select Distinct C.client_id,
C.first_name,
C.last_name,
CV.rev_timein,
Q1.answer,
Q2.answer,
Q3.answer
From Clients C,
(
Select C2.client_id as client_id,
Answer.answer as answer
From Clients C2
Inner Join ClientVisit On C2.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
Where SavedVisitAnswer.question_id = '518722' And Forms.form_id = '336' And
FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
ClientVisit.rev_timein < DateAdd(d, 1, @param2)
GROUP BY C2.client_id, Answer.answer
) Q1,
(
Select C3.client_id as client_id,
Answer.answer as answer
From Clients C3
Inner Join ClientVisit On C3.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
Where SavedVisitAnswer.question_id = '518727' And Forms.form_id = '336' And
FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
ClientVisit.rev_timein < DateAdd(d, 1, @param2)
GROUP BY C3.client_id, Answer.answer
) Q2,
(
Select C4.client_id as client_id,
Answer.answer as answer
From Clients C4
Inner Join ClientVisit On C4.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
Where SavedVisitAnswer.question_id = '518728' And Forms.form_id = '336' And
FormVersion.is_active = '1' And ClientVisit.rev_timein >= @param1 And
ClientVisit.rev_timein < DateAdd(d, 1, @param2)
GROUP BY C4.client_id, Answer.answer
) Q3
Inner Join ClientVisit CV On C.client_id = ClientVisit.client_id
Left Join SavedVisitAnswer On SavedVisitAnswer.clientvisit_id =
ClientVisit.clientvisit_id
Left Join Question On Question.question_id = SavedVisitAnswer.question_id
Left Join Category On Question.category_id = Category.category_id
Left Join FormVersion On Category.form_ver_id = FormVersion.form_ver_id
Left Join Forms On Forms.form_id = FormVersion.form_id
Inner Join Answer On SavedVisitAnswer.answer_id = Answer.answer_id
Where Forms.form_id = '336' And
FormVersion.is_active = '1' And CV.rev_timein >= @param1 And
CV.rev_timein < DateAdd(d, 1, @param2)
Order By C.last_name
目标是在日期范围内将每个客户的输出格式设置为这样。
Client_ID,名字,姓氏,日期时间,答案1,答案2,答案3
答案 0 :(得分:0)
您不需要对所需内容进行三个子查询。另外,您不需要在最后的where子句中进行过滤,因为您已经在子查询中进行了过滤。我试图为您创建简单的解决方案。如果您针对三个特定问题搜索三个答案,那么它应该对您有用:
DECLARE @DateFrom DATE
DECLARE @DateTo DATE
DECLARE @QuestionId1 INT
DECLARE @QuestionId2 INT
DECLARE @QuestionId3 INT
DECLARE @FormId INT
SET @DateFrom ='2018-11-01'
SET @DateTo ='2018-11-30'
SET @FormId =336
SET @QuestionId1 =518722
SET @QuestionId2 =518727
SET @QuestionId3 =518728
SELECT
C.client_id
,C.first_name
,C.last_name
,ClientVisit.rev_timein
,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId1 THEN Answer.Answer ELSE '' END) Answer1
,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId2 THEN Answer.Answer ELSE '' END) Answer2
,MAX(CASE WHEN SavedVisitAnswer.question_id = @QuestionId3 THEN Answer.Answer ELSE '' END) Answer3
FROM Clients C
INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
INNER JOIN Category ON Question.category_id = Category.category_id
INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
WHERE Forms.form_id = @FormId
AND FormVersion.is_active = '1'
AND ClientVisit.rev_timein >= @DateFrom
AND ClientVisit.rev_timein < DATEADD(DAY, 1, @DateTo)
GROUP BY
C.client_id
,C.first_name
,C.last_name
,ClientVisit.rev_timein
编辑,我删除了所有变量。包括您的@ param1和@ param2。如果要使用它们,请放回去执行它们:
SELECT
C.client_id
,C.first_name
,C.last_name
,ClientVisit.rev_timein
,MAX(CASE WHEN SavedVisitAnswer.question_id = 518722 THEN Answer.Answer ELSE '' END) Answer1
,MAX(CASE WHEN SavedVisitAnswer.question_id = 518727 THEN Answer.Answer ELSE '' END) Answer2
,MAX(CASE WHEN SavedVisitAnswer.question_id = 518728 THEN Answer.Answer ELSE '' END) Answer3
FROM Clients C
INNER JOIN ClientVisit ON C.client_id = ClientVisit.client_id
INNER JOIN SavedVisitAnswer ON SavedVisitAnswer.clientvisit_id = ClientVisit.clientvisit_id
INNER JOIN Question ON Question.question_id = SavedVisitAnswer.question_id
INNER JOIN Category ON Question.category_id = Category.category_id
INNER JOIN FormVersion ON Category.form_ver_id = FormVersion.form_ver_id
INNER JOIN Forms ON Forms.form_id = FormVersion.form_id
INNER JOIN Answer ON SavedVisitAnswer.answer_id = Answer.answer_id
WHERE Forms.form_id = 336
AND FormVersion.is_active = '1'
AND ClientVisit.rev_timein >= '2018-11-01' /*@param1?*/
AND ClientVisit.rev_timein < DATEADD(DAY, 1, '2018-11-30' /*param2?*/)
GROUP BY
C.client_id
,C.first_name
,C.last_name
,ClientVisit.rev_timein