이번엔 질병 코드로 분석 대상을 정해보자. MIMIC-III의 d_icd_diagnoses 테이블은 진단 코드에 대한 데이터가 적재되어 있는 차원(dimension)테이블이다. 테이블은 아래와 같이 구성되어 있다.
mimic=> \\d d_icd_diagnoses
Table "public.d_icd_diagnoses"
Column | Type | Collation | Nullable | Default
-------------+------------------------+-----------+----------+---------
row_id | integer | | not null |
icd9_code | character varying(10) | | not null |
short_title | character varying(50) | | not null |
long_title | character varying(255) | | not null |
icd9 진단 코드 중 sepsis가 들어간 데이터를 찾아보자. psql의 ILIKE를 사용하면 대소문자를 가리지 않고 데이터를 검색할 수 있다.
-- SQL 3.6
SELECT *
FROM d_icd_diagnoses
WHERE short_title ILIKE '%sepsis%';
row_id | icd9_code | short_title | long_title
--------+-----------+--------------------------+-----------------------------------------------------------------------
9050 | 77181 | NB septicemia [sepsis] | Septicemia [sepsis] of newborn
11403 | 99591 | Sepsis | Sepsis
11404 | 99592 | Severe sepsis | Severe sepsis
13564 | 67020 | Puerperal sepsis-unsp | Puerperal sepsis, unspecified as to episode of care or not applicable
13565 | 67022 | Puerprl sepsis-del w p/p | Puerperal sepsis, delivered, with mention of postpartum complication
13566 | 67024 | Puerperl sepsis-postpart | Puerperal sepsis, postpartum condition or complication
(6 rows)
SQL 3.6에서 구한 패혈증 관련 icd9_code를 기준으로 패혈증에 걸린 환자를 찾아보자. MIMIC-III의 diagnoses_icd 테이블은 환자들이 입원부터 퇴원까지 진단되었던 모든 질병에 대한 코드를 데이터로 보여준다. 테이블 구성은 아래와 같다.
mimic=> \\d diagnoses_icd
Table "public.diagnoses_icd"
Column | Type | Collation | Nullable | Default
------------+-----------------------+-----------+----------+---------
row_id | integer | | not null |
subject_id | integer | | not null |
hadm_id | integer | | not null |
seq_num | integer | | |
icd9_code | character varying(10) | | |
아래는 패혈증에 걸린 환자를 찾는 SQL이다.
-- SQL 3.7
SELECT *
FROM diagnoses_icd
WHERE icd9_code IN (SELECT icd9_code FROM d_icd_diagnoses WHERE short_title ILIKE '%sepsis%') -- 해석 1번
row_id | subject_id | hadm_id | seq_num | icd9_code
--------+------------+---------+---------+-----------
726 | 92 | 142807 | 2 | 77181
737 | 93 | 160481 | 4 | 77181
1444 | 110 | 154943 | 2 | 77181
5670 | 474 | 194246 | 3 | 77181
...
631694 | 95803 | 134466 | 5 | 99592
638356 | 97143 | 122472 | 10 | 99592
(5409 rows)
SQL 해석