我有一份召回报告,根据他们的召回计划(根据他们的情况确定何时应该返回的计划)为每个需要在特定日期返回办公室的患者出示信件。我有另一张表,存储每位患者(过去和现在)的所有预约。 recall_plans表中的约会是自动生成的,而约会表中的约会是手动创建的。如果一个人打电话进行预约,则不会检查召回计划报告,因此两个表中都会出现相同的约会,导致发出重复的提醒信。
我需要做两件事: (我知道我的方法不一定能解决业务问题,但这就是我的任务)
召回报告查询:
SELECT description as [Plan Name],
per.first_name + ' ' + per.last_name as [Patient],
substring (plan_start_date, 5,2) + '-' +
substring (plan_start_date, 7,2) + '-' +
substring (plan_start_date, 1,4) as [Plan Start Date],
substring (nr.expected_return_date, 5,2) + '-' +
substring (nr.expected_return_date, 7,2) + '-' +
substring (nr.expected_return_date, 1,4) as [Expected Return Date]
FROM recall_plan_mstr rp,
patient_recall_plans nr,
patient pt,
person per
WHERE rp.practice_id = nr.practice_id
and rp.recall_plan_id = nr.recall_plan_id
and nr.practice_id = pt.practice_id
and nr.person_id = pt.person_id
and per.person_id = pt.person_id
and (active_plan_ind = 'Y')
and rp.practice_id = '0025'
召回报告结果:
PLAN NAME PATIENT START RETURN
------------------ ---------------- ---------- ----------
OFFICE VISIT W/ DR Charles Span 04-18-2011 12-15-2011
LIPID PANEL Ronald Chap 04-11-2011 06-28-2011
OFFICE VISIT W/ DR Ronald Chap 04-11-2011 04-21-2011
OFFICE VISIT W/ DR Will Thor 03-31-2011 02-01-2012
PACEMAKER CHECK Sylvia Berkly 05-03-2011 08-03-2011
OFFICE VISIT W/ DR Tim Cayle 04-13-2011 09-26-2011
OFFICE VISIT W/ DR Caferana Mercade 04-11-2011 10-08-2011
OFFICE VISIT W/ DR Susanna Calter 05-10-2011 05-07-2012
ICD CHECK Jim Southern 04-14-2011 07-13-2011
STRESS ECHO Don Cobey 04-28-2011 06-07-2010
约会查询:
select person_id, appt_date
from appointments
where person_id is not null
group by person_id, appt_date
order by person_id, appt_date desc
约会结果:
person_id appt_date
------------------------------------ ---------
073C8F83-CE15-4192-8E12-00006CB5A433 20091228
073C8F83-CE15-4192-8E12-00006CB5A433 20090510
073C8F83-CE15-4192-8E12-00006CB5A433 20090301
073C8F83-CE15-4192-8E12-00006CB5A433 20081006
378A281C-FAE7-43DF-BC03-00006E386680 20110509
378A281C-FAE7-43DF-BC03-00006E386680 20110217
378A281C-FAE7-43DF-BC03-00006E386680 20110124
378A281C-FAE7-43DF-BC03-00006E386680 20110111
378A281C-FAE7-43DF-BC03-00006E386680 20101207
816D4D31-3C99-4762-878D-000097883B73 20110316
816D4D31-3C99-4762-878D-000097883B73 20101216
问题:
我希望我已经充分解释并提供了足够的信息。如果需要任何其他信息,请不要犹豫。
答案 0 :(得分:1)
您的第一个问题的答案如下:
SELECT person_id, min(appt_date) as appt_date
FROM appointments
WHERE person_id is not null
AND now() < appt_date -- This line is database specific
GROUP BY person_id
ORDER BY person_id
你的第二个问题的答案是你需要一个左连接。如果在任何地方使用连接语法,则左连接会更容易。这是查询。
SELECT description as [Plan Name]
, per.first_name + ' ' + per.last_name as [Patient]
, substring (plan_start_date, 5,2) + '-' +
substring (plan_start_date, 7,2) + '-' +
substring (plan_start_date, 1,4) as [Plan Start Date]
, substring (nr.expected_return_date, 5,2) + '-' +
substring (nr.expected_return_date, 7,2) + '-' +
substring (nr.expected_return_date, 1,4) as [Expected Return Date]
, CASE
WHEN next_appt.appt_date IS NULL
THEN ''
ELSE substring (next_appt.appt_date, 5,2) + '-' +
substring (next_appt.appt_date, 7,2) + '-' +
substring (next_appt.appt_date, 1,4)
END as [Next Appointment Date]
FROM recall_plan_mstr rp
JOIN patient_recall_plans nr
ON rp.recall_plan_id = nr.recall_plan_id
JOIN patient pt
ON nr.practice_id = pt.practice_id
AND nr.person_id = pt.person_id
JOIN person per
ON and per.person_id = pt.person_id
LEFT JOIN (
SELECT person_id, min(appt_date) as appt_date
FROM appointments
WHERE person_id is not null
AND now() < appt_date -- This line is database specific
GROUP BY person_id
ORDER BY person_id
) next_appt
ON next_appt.person_id = pt.person_id
WHERE (active_plan_ind = 'Y')
AND rp.practice_id = '0025'
您会注意到我将查询格式化为易读性。请参阅http://bentilly.blogspot.com/2011/02/sql-formatting-style.html,了解我选择以我的方式格式化SQL的原因。