-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_patients_table.sql
More file actions
64 lines (57 loc) · 1.14 KB
/
create_patients_table.sql
File metadata and controls
64 lines (57 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-- Combines admissions, patients, and transfer table
CREATE OR REPLACE TABLE pat_tbl_combo AS
SELECT
a.row_id,
a.subject_id,
a.hadm_id,
a.admission_type,
a.admission_location,
a.discharge_location,
a.insurance,
a.language,
a.religion,
a.marital_status,
a.ethnicity,
a.diagnosis,
p.gender,
p.dob,
p.dod,
t.eventtype
FROM
admissions a
JOIN patients p ON
p.subject_id = a.subject_id
JOIN transfers t ON
t.subject_id = p.subject_id;
-- Adds age column
ALTER TABLE pat_tbl_combo ADD COLUMN age int;
UPDATE
pat_tbl_combo
SET
age = (EXTRACT(DAY
FROM
(dod - dob))::FLOAT) / 365.25;
-- Removes dob and dod columns
ALTER TABLE pat_tbl_combo DROP COLUMN dob;
ALTER TABLE pat_tbl_combo DROP COLUMN dod;
-- Deletes patients that have an age greater than 100 (outliers)
DELETE
FROM
pat_tbl_combo
WHERE
age > 100;
-- Adds age_group bin column
ALTER TABLE pat_tbl_combo ADD COLUMN age_group VARCHAR(3);
UPDATE
pat_tbl_combo
SET
age_group = CASE
WHEN age < 30 THEN '20s'
WHEN age < 40 THEN '30s'
WHEN age < 50 THEN '40s'
WHEN age < 60 THEN '50s'
WHEN age < 70 THEN '60s'
WHEN age < 80 THEN '70s'
WHEN age < 90 THEN '80s'
ELSE '90s'
END;