-
Notifications
You must be signed in to change notification settings - Fork 1
/
demographic_analysis.sql
59 lines (54 loc) · 1.27 KB
/
demographic_analysis.sql
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
/*
* Identify demographic characteristic for patients
*/
-- find sex distribution
SELECT
sex,
count(*),
count(*) / 122.0 * 100
FROM
demographic_all da
JOIN consented c ON c.record_id = da.patid
WHERE
c.ehr_access_agree = 1
GROUP BY
sex;
-- find race distribution
SELECT
lpad(race, 2, '0'),
count(*),
count(*) / 122.0 * 100
FROM
demographic_all da
JOIN consented c ON c.record_id = da.patid
WHERE
c.ehr_access_agree = 1
GROUP BY
lpad(race, 2, '0')
ORDER BY
count(*) DESC;
;
-- find hispanic distribution
SELECT
hispanic,
count(*),
count(*) / 122.0 * 100
FROM
demographic_all da
JOIN consented c ON c.record_id = da.patid
WHERE
c.ehr_access_agree = 1
GROUP BY
hispanic;
-- find age distribution
SELECT
min(date_part('year', CURRENT_DATE::timestamp) - date_part('year', d.birth_date::timestamp)),
max(date_part('year', CURRENT_DATE::timestamp) - date_part('year', d.birth_date::timestamp)),
avg(date_part('year', CURRENT_DATE::timestamp) - date_part('year', d.birth_date::timestamp))
age,
stddev(date_part('year', CURRENT_DATE::timestamp) - date_part('year', d.birth_date::timestamp))
FROM
demographic_all d
JOIN consented c ON c.record_id = d.patid
WHERE
c.ehr_access_agree = 1;