-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal_query.sql
More file actions
91 lines (89 loc) · 3.11 KB
/
original_query.sql
File metadata and controls
91 lines (89 loc) · 3.11 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
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
use assignment02;
show tables;
select * from dataset_med;
explain analyze
SELECT
fp.cancer_stage,
fp.treatment_type,
(SELECT COUNT(*)
FROM dataset_med p
WHERE
p.survived = 1
AND p.asthma = 0
AND p.cirrhosis = 0
AND p.age > 40
AND p.diagnosis_date > '2018-01-01'
AND p.cancer_stage IS NOT NULL
AND p.treatment_type IS NOT NULL
AND p.cancer_stage = fp.cancer_stage
AND p.treatment_type = fp.treatment_type) AS survivors_count,
(SELECT AVG(p.bmi * 1.0)
FROM dataset_med p
WHERE
p.survived = 1
AND p.asthma = 0
AND p.cirrhosis = 0
AND p.age > 40
AND p.diagnosis_date > '2018-01-01'
AND p.cancer_stage IS NOT NULL
AND p.treatment_type IS NOT NULL
AND p.cancer_stage = fp.cancer_stage
AND p.treatment_type = fp.treatment_type) AS avg_bmi,
(SELECT AVG(p.cholesterol_level * 1.0)
FROM dataset_med p
WHERE
p.survived = 1
AND p.asthma = 0
AND p.cirrhosis = 0
AND p.age > 40
AND p.diagnosis_date > '2018-01-01'
AND p.cancer_stage IS NOT NULL
AND p.treatment_type IS NOT NULL
AND p.cancer_stage = fp.cancer_stage
AND p.treatment_type = fp.treatment_type) AS avg_cholesterol,
CASE
WHEN (SELECT AVG(p.age * 1.0)
FROM dataset_med p
WHERE
p.survived = 1
AND p.asthma = 0
AND p.cirrhosis = 0
AND p.age > 40
AND p.diagnosis_date > '2018-01-01'
AND p.cancer_stage IS NOT NULL
AND p.treatment_type IS NOT NULL
AND p.cancer_stage = fp.cancer_stage
AND p.treatment_type = fp.treatment_type) >= 60
THEN 'elderly'
WHEN (SELECT AVG(p.age * 1.0)
FROM dataset_med p
WHERE
p.survived = 1
AND p.asthma = 0
AND p.cirrhosis = 0
AND p.age > 40
AND p.diagnosis_date > '2018-01-01'
AND p.cancer_stage IS NOT NULL
AND p.treatment_type IS NOT NULL
AND p.cancer_stage = fp.cancer_stage
AND p.treatment_type = fp.treatment_type) BETWEEN 40 AND 59
THEN 'middle-aged'
ELSE 'young'
END AS age_group
FROM
(SELECT DISTINCT cancer_stage, treatment_type FROM dataset_med WHERE 1=1) fp
WHERE
(SELECT COUNT(*)
FROM dataset_med p
WHERE
p.survived = 1
AND p.asthma = 0
AND p.cirrhosis = 0
AND p.age > 40
AND p.diagnosis_date > '2018-01-01'
AND p.cancer_stage IS NOT NULL
AND p.treatment_type IS NOT NULL
AND p.cancer_stage = fp.cancer_stage
AND p.treatment_type = fp.treatment_type) > 10
ORDER BY
survivors_count DESC, avg_bmi DESC;