-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth-analytics.sql
139 lines (114 loc) · 3.38 KB
/
health-analytics.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
------------------------------
--Health Analysis Case study--
------------------------------
--Author: Subala singh
--Date: 31/05/2022
--Tool used: Postgre SQL Server
------------------------
--CASE STUDY QUESTIONS--
------------------------
-- 1. How many unique users exist in the logs dataset?
SELECT COUNT (DISTINCT id)
FROM health.user_logs;
-- Result:
+──────────+
|count |
+──────────+
|554 |
+──────────+
-- For question 2-8, I created a temporary table:
-- > Step 1: Firstly, I ran a DROP TABLE IF EXISTS statement to clear out any previously created tables:
DROP TABLE IF EXISTS user_measure_count;
-- > Step 2: Next, I created a new temporary table using the results of the query below:
CREATE TEMP TABLE user_measure_count AS
SELECT
id,
COUNT(*) AS measure_count,
COUNT (DISTINCT measure) AS unique_measures
FROM health.user_logs
GROUP BY 1;
-- 2. How many total measurements do we have per user on average?
SELECT
ROUND (AVG(measure_count), 2) AS mean_value
FROM user_measure_count;
--Result:
+────────────+
|mean_value |
+────────────+
|79.23 |
+────────────+
-- 3. What about the median number of measurements per user?
SELECT
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY measure_count) AS median_value
FROM user_measure_count;
--Result:
+─────────────+
|median_value |
+─────────────+
|2 |
+─────────────+
-- 4. How many users have 3 or more measurements?
SELECT COUNT(*)
FROM user_measure_count
WHERE measure_count >= 3;
--Result:
+────────+
|count |
+────────+
|209 |
+────────+
-- 5. How many users have 1,000 or more measurements?
SELECT COUNT(*)
FROM user_measure_count
WHERE measure_count >= 1000;
--Result:
+───────+
|count |
+───────+
|5 |
+───────+
-- 6. Have logged blood glucose measurements?
SELECT
COUNT(DISTINCT id)
FROM health.user_logs
WHERE measure = 'blood_glucose';
--Result:
+───────+
|count |
+───────+
|325 |
+───────+
-- 7. Have at least 2 types of measurements?
SELECT
COUNT(*)
FROM user_measure_count
WHERE unique_measures >= 2;
--Result:
+───────+
|count |
+───────+
|204 |
+───────+
-- 8. Have all 3 measures - blood glucose, weight and blood pressure?
SELECT
COUNT(*)
FROM user_measure_count
WHERE unique_measures = 3;
--Result:
+───────+
|count |
+───────+
|50 |
+───────+
-- 9. What is the median systolic/diastolic blood pressure values?
SELECT
PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY systolic) AS median_systolic,
PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY diastolic) AS median_diastolic
FROM health.user_logs
WHERE measure = 'blood_pressure';
--Result:
+─────────────────+──────────────────+
|median_systolic |median_diastolic |
+─────────────────+──────────────────+
|126 |79 |
+─────────────────+──────────────────+