2011-06-01 92 views
0

我有每个人谁需要回到办公室就根据自己的召回计划(决定的时候就应该返回根据自己的病情计划)在特定日期病人产生的信件了召回报告。我还有另一张表,用于存储每位患者(过去和现在)的所有约会。 recall_plans表中的约会是自动生成的,而约会表中的约会是手动创建的。如果一个人在电话预约所以往往是相同的约会导致重复催款两个表中的代表发送出去不检查召回计划报告。添加下一个约会日期

我需要做两件事情: (我知道我的方法不一定是解决业务问题,但是这是我与负责)

  1. 我需要制作展示 列表下一个约会为每个病人 但只有当它是在未来。
  2. 我需要添加一列到第一 报告显示每个患者的下一个 任命所以有人可以手动 识别重复的字母 会出去针对特定患者 并相应地进行干预。

召回报告查询:

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 

问题:

  1. 我怎么能生产出从 预约表与 一个病人结果每行仅是在 未来 最新任命名单?我是否需要写一个光标 是什么?
  2. 我怎么能这样搀入名单我 召回报告,因此有一列 收益栏的右侧是 显示患者的下一个 预约日期(仅适用于未来的)?这两个 表有一个人数量GUID。

我希望我已经充分解释和提供足够的信息。如果需要任何其他信息,请不要犹豫,问。

回答

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。