-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmeasurement_of_interest.sql
59 lines (56 loc) · 1.35 KB
/
measurement_of_interest.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
-- Return row level data for a measurement for our cohort.
--
-- PARAMETERS:
-- MEASUREMENT_CONCEPT_ID: for example 3004410 # Hemoglobin A1c
-- UNIT_CONCEPT_ID: for example 8554 # percent
WITH
--
-- Retrieve participants birthdate and sex_at_birth.
--
persons AS (
SELECT
person_id,
birth_datetime,
concept_name AS sex_at_birth
FROM
`{CDR}.person`
LEFT JOIN `{CDR}.concept` ON concept_id = sex_at_birth_concept_id),
--
-- Retrieve the row-level data for our measurement of interest.
--
measurements AS (
SELECT
person_id,
measurement_id,
measurement_concept_id,
measurement_date,
measurement_datetime,
measurement_type_concept_id,
operator_concept_id,
value_as_number,
value_as_concept_id,
unit_concept_id,
range_low,
range_high
FROM
`{CDR}.measurement`
WHERE
measurement_concept_id = {MEASUREMENT_CONCEPT_ID}
AND unit_concept_id = {UNIT_CONCEPT_ID}
AND person_id IN ({COHORT_QUERY}))
--
-- Lastly, JOIN all this data together so that we have the birthdate, sex_at_birth and site for each measurement.
--
SELECT
persons.*,
src_id,
measurements.* EXCEPT(person_id, measurement_id)
FROM
measurements
LEFT JOIN
persons USING (person_id)
LEFT JOIN
`{CDR}.measurement_ext` USING (measurement_id)
ORDER BY
person_id,
measurement_id