-
Notifications
You must be signed in to change notification settings - Fork 97
/
field_plausible_temporal_after.sql
executable file
·63 lines (60 loc) · 2.07 KB
/
field_plausible_temporal_after.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
/*********
PLAUSIBLE_TEMPORAL_AFTER
get number of records and the proportion to total number of eligible records with datetimes that do not occur on or after their corresponding datetimes
Parameters used in this template:
cdmDatabaseSchema = @cdmDatabaseSchema
cdmTableName = @cdmTableName
cdmFieldName = @cdmFieldName
plausibleTemporalAfterTableName = @plausibleTemporalAfterTableName
plausibleTemporalAfterFieldName = @plausibleTemporalAfterFieldName
{@cohort & '@runForCohort' == 'Yes'}?{
cohortDefinitionId = @cohortDefinitionId
cohortDatabaseSchema = @cohortDatabaseSchema
}
**********/
SELECT
num_violated_rows,
CASE
WHEN denominator.num_rows = 0 THEN 0
ELSE 1.0*num_violated_rows/denominator.num_rows
END AS pct_violated_rows,
denominator.num_rows AS num_denominator_rows
FROM
(
SELECT
COUNT_BIG(violated_rows.violating_field) AS num_violated_rows
FROM
(
/*violatedRowsBegin*/
SELECT
'@cdmTableName.@cdmFieldName' AS violating_field,
cdmTable.*
FROM @cdmDatabaseSchema.@cdmTableName cdmTable
{@cdmDatabaseSchema.@cdmTableName != @cdmDatabaseSchema.@plausibleTemporalAfterTableName}?{
JOIN @cdmDatabaseSchema.@plausibleTemporalAfterTableName plausibleTable ON cdmTable.person_id = plausibleTable.person_id}
{@cohort & '@runForCohort' == 'Yes'}?{
JOIN @cohortDatabaseSchema.cohort c ON cdmTable.person_id = c.subject_id
AND c.cohort_definition_id = @cohortDefinitionId
}
WHERE
{'@plausibleTemporalAfterTableName' == 'PERSON'}?{
COALESCE(
CAST(plausibleTable.@plausibleTemporalAfterFieldName AS DATE),
CAST(CONCAT(plausibleTable.year_of_birth,'-06-01') AS DATE)
)
}:{
CAST(cdmTable.@plausibleTemporalAfterFieldName AS DATE)
} > CAST(cdmTable.@cdmFieldName AS DATE)
/*violatedRowsEnd*/
) violated_rows
) violated_row_count,
(
SELECT
COUNT_BIG(*) AS num_rows
FROM @cdmDatabaseSchema.@cdmTableName cdmTable
{@cohort & '@runForCohort' == 'Yes'}?{
JOIN @cohortDatabaseSchema.cohort c ON cdmTable.person_id = c.subject_id
AND c.cohort_definition_id = @cohortDefinitionId
}
) denominator
;