drh_time_range_stacked_metrics
Column |
Type |
participant_id |
TEXT |
time_below_range_low_percentage |
|
time_below_range_low |
|
time_below_range_low_string |
|
time_below_range_very_low_percentage |
|
time_below_range_very_low |
|
time_below_range_very_low_string |
|
time_in_range_percentage |
|
time_in_range |
|
time_in_range_string |
|
time_above_vh_percentage |
|
time_above_vh |
|
time_above_vh_string |
|
time_above_range_high_percentage |
|
time_above_range_high |
|
time_above_range_high_string |
|
SQL DDL
CREATE VIEW drh_time_range_stacked_metrics AS
WITH GlucoseMetrics AS (
SELECT
participant_id,
COUNT(*) AS total_readings,
SUM(CASE WHEN CGM_Value BETWEEN 54 AND 69 THEN 1 ELSE 0 END) AS time_below_range_low,
SUM(CASE WHEN CGM_Value < 54 THEN 1 ELSE 0 END) AS time_below_range_very_low,
SUM(CASE WHEN CGM_Value BETWEEN 70 AND 180 THEN 1 ELSE 0 END) AS time_in_range,
SUM(CASE WHEN CGM_Value > 250 THEN 1 ELSE 0 END) AS time_above_vh,
SUM(CASE WHEN CGM_Value BETWEEN 181 AND 250 THEN 1 ELSE 0 END) AS time_above_range_high
FROM
combined_cgm_tracing
GROUP BY
participant_id
), Defaults AS (
SELECT
0 AS total_readings,
0 AS time_below_range_low,
0 AS time_below_range_very_low,
0 AS time_in_range,
0 AS time_above_vh,
0 AS time_above_range_high
)
SELECT
gm.participant_id,
COALESCE(CASE WHEN gm.total_readings = 0 THEN 0 ELSE (gm.time_below_range_low * 100.0 / gm.total_readings) END, 0) AS time_below_range_low_percentage,
COALESCE(gm.time_below_range_low, 0) AS time_below_range_low,
COALESCE(CASE WHEN gm.total_readings = 0 THEN '00 hours, 00 minutes' ELSE printf('%02d hours, %02d minutes', (gm.time_below_range_low * 5) / 60, (gm.time_below_range_low * 5) % 60) END, '00 hours, 00 minutes') AS time_below_range_low_string,
COALESCE(CASE WHEN gm.total_readings = 0 THEN 0 ELSE (gm.time_below_range_very_low * 100.0 / gm.total_readings) END, 0) AS time_below_range_very_low_percentage,
COALESCE(gm.time_below_range_very_low, 0) AS time_below_range_very_low,
COALESCE(CASE WHEN gm.total_readings = 0 THEN '00 hours, 00 minutes' ELSE printf('%02d hours, %02d minutes', (gm.time_below_range_very_low * 5) / 60, (gm.time_below_range_very_low * 5) % 60) END, '00 hours, 00 minutes') AS time_below_range_very_low_string,
COALESCE(CASE WHEN gm.total_readings = 0 THEN 0 ELSE (gm.time_in_range * 100.0 / gm.total_readings) END, 0) AS time_in_range_percentage,
COALESCE(gm.time_in_range, 0) AS time_in_range,
COALESCE(CASE WHEN gm.total_readings = 0 THEN '00 hours, 00 minutes' ELSE printf('%02d hours, %02d minutes', (gm.time_in_range * 5) / 60, (gm.time_in_range * 5) % 60) END, '00 hours, 00 minutes') AS time_in_range_string,
COALESCE(CASE WHEN gm.total_readings = 0 THEN 0 ELSE (gm.time_above_vh * 100.0 / gm.total_readings) END, 0) AS time_above_vh_percentage,
COALESCE(gm.time_above_vh, 0) AS time_above_vh,
COALESCE(CASE WHEN gm.total_readings = 0 THEN '00 hours, 00 minutes' ELSE printf('%02d hours, %02d minutes', (gm.time_above_vh * 5) / 60, (gm.time_above_vh * 5) % 60) END, '00 hours, 00 minutes') AS time_above_vh_string,
COALESCE(CASE WHEN gm.total_readings = 0 THEN 0 ELSE (gm.time_above_range_high * 100.0 / gm.total_readings) END, 0) AS time_above_range_high_percentage,
COALESCE(gm.time_above_range_high, 0) AS time_above_range_high,
COALESCE(CASE WHEN gm.total_readings = 0 THEN '00 hours, 00 minutes' ELSE printf('%02d hours, %02d minutes', (gm.time_above_range_high * 5) / 60, (gm.time_above_range_high * 5) % 60) END, '00 hours, 00 minutes') AS time_above_range_high_string
FROM
Defaults d
LEFT JOIN GlucoseMetrics gm ON 1=1