我有一个由患者及其合并症(或通常被称为诊断代码)组成的假设数据库:
PATIENT_ID | HOSPITAL | DIAG_01 | DIAG_02 | DIAG_03 | DIAG_04 ...up to Diag_14
Patient 1 | Hosp1 | C01 | C02 | NULL | NULL
Patient 2 | Hosp1 | A01 | A02 | A03 | P37
Patient 3 | Hosp2 | A01 | D10 | X01 | NULL
患者合并症(其中最多可记录14例)定义患者可能患有的任何疾病/疾病。特别是C00-C97和D00-D48涉及癌症ICD-10代码。
我想为每家医院制作一份摘要,其中列出了有多少患者在诊断栏中有癌症ICD-10代码的情况。
基于上面的例子,Hosp1和Hosp2都有1名患者,每名患有癌症。患者1的C01和C02诊断代码仅计为一名患有癌症的患者。然而,如果患者1再次入院,那将被视为癌症患者的另一例。
我尝试创建一个包含所有癌症合并症的表格,并在所有14个诊断列上使用JOIN查询,但无法计算。
任何想法或帮助都会受到大量的重视。
答案 0 :(得分:1)
您的数据布局不正确。你应该有一个联结表。但是,你不是。您可以使用union all
:
select hospital, count(distinct patient_id)
from ((select patient_id, hospital, diag_01 as diag
from hypothetical
) union all
(select patient_id, hospital, diag_02 as diag
from hypothetical
)
union all
. . .
(select patient_id, hospital, diag_14 as diag
from hypothetical
)
) phd
where diag in ('C01', 'C02');
答案 1 :(得分:1)
使用union all
SELECT * FROM /**customize your select here could be count() etc **/
(SELECT PATIENT_ID,HOSPITAL,DIAGNOSIS
FROM (
SELECT PATIENT_ID, HOSPITAL, LEFT(DIAG_01,1) AS DIAGNOSIS FROM table_name WHERE DIAG_01 IS NOT NULL UNION ALL
SELECT PATIENT_ID, HOSPITAL, LEFT(DIAG_02,1) AS DIAGNOSIS FROM table_name WHERE DIAG_02 IS NOT NULL UNION ALL
SELECT PATIENT_ID, HOSPITAL, LEFT(DIAG_03,1) AS DIAGNOSIS FROM table_name WHERE DIAG_03 IS NOT NULL UNION ALL
SELECT PATIENT_ID, HOSPITAL, LEFT(DIAG_04,1) AS DIAGNOSIS FROM table_name WHERE DIAG_04 IS NOT NULL
) AS T1
GROUP BY PATIENT_ID,HOSPITAL,DIAGNOSIS
) AS T2
WHERE DIAGNOSIS IN ('C','D') /**where **/