2011-06-01 86 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。