假設,我有一張表格,上面有患者名單和手術日期。
ID_PATIENT SURG_DATE
xxxxxxxxx1 07MAR2006:00:00:00
xxxxxxxxx2 11FEB2006:00:00:00
xxxxxxxxx3 14JAN2006:00:00:00
xxxxxxxxx4 01JAN2005:00:00:00
假設,我有第二張表,其中列出了與某種治療型別相對應的藥物代碼(例如糖尿病治療)
MED_CODE
3484027
3484028
現在,我有一張桌子,上面有所有藥物和配藥的日期。
ID_PATIENT MED_CODE DEL_DATE
xxxxxxxxx1 3484027 29DEC2005:00:00:00
xxxxxxxxx1 3484028 12JUN2005:00:00:00
xxxxxxxxx2 3484027 10JAN2005:00:00:00
xxxxxxxxx2 1234567 10MAR2005:00:00:00
xxxxxxxxx2 3484027 14APR2005:00:00:00
xxxxxxxxx3 3484027 12FEB2005:00:00:00
xxxxxxxxx3 3484028 14AUG2005:00:00:00
xxxxxxxxx3 3484027 17NOV2005:00:00:00
xxxxxxxxx4 3484027 17NOV2004:00:00:00
xxxxxxxxx4 3484027 20NOV2004:00:00:00
xxxxxxxxx4 3484027 13JAN2005:00:00:00
我正在嘗試做的是在我的患者表中創建一個新列,指示患者在手術前一年內是否至少服用了 3 次藥物。因此,在示例資料中,只有第三位患者符合要求,因為:
- 第一個病人只接受了兩次治療。
- 第二位患者服用了三種藥物,但其中一種不在糖尿病清單中。
- 第四位患者服用了三種藥物,但其中一種是在手術后服用的。
以下是在 PL/SQL 中生成示例資料的代碼:
create table PATIENTS
(
ID_PATIENT varchar2(10)
, SURG_DATE date
)
insert into PATIENTS values ('xxxxxxxxx1', to_date('20060307', 'yyyymmdd'))
insert into PATIENTS values ('xxxxxxxxx2', to_date('20060211', 'yyyymmdd'))
insert into PATIENTS values ('xxxxxxxxx3', to_date('20060114', 'yyyymmdd'))
insert into PATIENTS values ('xxxxxxxxx4', to_date('20050101', 'yyyymmdd'))
create table DIABETES_MEDS
(
MED_CODE varchar2(10)
)
insert into DIABETES_MEDS values ('3484027')
insert into DIABETES_MEDS values ('3484028')
create table MEDS
(
ID_PATIENT varchar2(10)
, MED_CODE varchar2(7)
, DEL_DATE date
)
insert into MEDS values ('xxxxxxxxx1', '3484027', to_date('20051229', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx1', '3484028', to_date('20050612', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx2', '3484027', to_date('20050110', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx2', '1234567', to_date('20050310', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx2', '3484027', to_date('20050414', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx3', '3484027', to_date('20050212', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx3', '3484028', to_date('20050814', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx3', '3484027', to_date('20051117', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx4', '3484027', to_date('20041117', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx4', '3484027', to_date('20041120', 'yyyymmdd'))
insert into MEDS values ('xxxxxxxxx4', '3484027', to_date('20050113', 'yyyymmdd'))
uj5u.com熱心網友回復:
這將為您提供滿足條件的患者:
SELECT COUNT(1), m.id_patient
FROM meds m
, patients p
WHERE m.id_patient = p.id_patient
AND m.med_code IN (SELECT med_code FROM diabetes_meds)
AND m.del_date >= ADD_MONTHS(p.surg_date,-12)
AND m.del_date <= p.surg_date
GROUP BY m.id_patient
HAVING COUNT(1) >= 3;
創建新列后,您可以使用與MERGE此類似的內容填充它:
MERGE INTO patients p
USING ( SELECT COUNT(1), m.id_patient
FROM meds m
, patients p
WHERE m.id_patient = p.id_patient
AND m.med_code IN (SELECT med_code FROM diabetes_meds)
AND m.del_date >= ADD_MONTHS(p.surg_date,-12)
AND m.del_date <= p.surg_date
GROUP BY m.id_patient
HAVING COUNT(1) >= 3 ) meds
ON (p.id_patient = meds.id_patient)
WHEN MATCHED THEN UPDATE SET <p.had_meds> = 'Y'; -- or whatever type of flag you want to use
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484720.html
下一篇:資料集中不同階段的時間差
