-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweek4-solutions.qmd
More file actions
121 lines (102 loc) · 2.61 KB
/
week4-solutions.qmd
File metadata and controls
121 lines (102 loc) · 2.61 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
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
---
title: "Week 4 Exercises - Solutions"
---
We'll first connect to the database:
```{r}
#| context: setup
library(duckdb)
library(DBI)
library(DiagrammeR)
con <- DBI::dbConnect(duckdb::duckdb(),
"data/GiBleed_5.3_1.1.duckdb")
```
## Subquery in `SELECT`
1. Fill in the blank in the subquery below to find each patient's demographic data along with the **total number of procedures** they have had. Note that this query makes use of the `person` table as well as the `procedure_occurrence` table.
```{sql connection="con"}
SELECT
person_id,
gender_source_value,
race_source_value,
(SELECT
COUNT(*)
FROM
procedure_occurrence
WHERE
person.person_id = procedure_occurrence.person_id
) AS number_of_procedures
FROM
person;
```
2. Fill in the blank in the query below to dynamically calculate the **number of months** between the **procedure date** and today for all procedures from the `procedure_occurrence` table
```{sql connection="con"}
#| eval: false
SELECT
person_id,
visit_occurrence_id,
procedure_occurrence_id,
procedure_concept_id,
procedure_datetime,
(SELECT
DATE_DIFF(
'month', procedure_datetime, DATE '2025-03-07'
)
) AS procedure_time_to_today
FROM
procedure_occurrence;
```
## Subquery in `WHERE`
3. Collect patient demographic data for all patients who have an occurrence of a condition with id = "40481087":
```{sql connection="con"}
SELECT
person_id,
birth_datetime,
gender_source_value,
race_source_value,
ethnicity_source_value
FROM
person
WHERE
person_id IN (
SELECT
person_id
FROM
condition_occurrence
WHERE
condition_concept_id == '40481087'
);
```
## Creating a view
4. Create a view for senior citizen demographics, where we collect demographics for patients born in or before 1960.
```{sql}
#| connection: "con"
CREATE VIEW senior_demographics AS
SELECT
person_id,
birth_datetime,
gender_source_value,
race_source_value,
ethnicity_source_value
FROM person
WHERE
year_of_birth >= '1960';
```
## Challenge: Creating a view (using `DATEDIFF` in a subquery)
5. Create a view for senior citizen procedures, where we collect procedure occurrences for all patients aged \>= 50 at the time of their procedure
```{sql}
#| connection: "con"
CREATE VIEW senior_procedures AS
SELECT *
FROM procedure_occurrence
WHERE procedure_occurrence.person_id IN (
SELECT
person_id
FROM
person
WHERE
DATE_DIFF(
'year',
person.birth_datetime,
procedure_occurrence.procedure_datetime
) >= 50
);
```