|
| 1 | +# Extract durations of Octreotide intake |
| 2 | + |
| 3 | +The data extraction process of Octreotide intake (`itemid = 225155`) turned out to be complex, considering that patients receive prescribed fluids potential interruptions or dosage changes. Therefore, rows in which a previous administration was immediately continued were joined by window functions, group-wise enumerations, and conditional expressions. We describe this in the following with obfuscated data. |
| 4 | + |
| 5 | +Select start and end times, having grouped by `linkorderid`: |
| 6 | + |
| 7 | +```sql |
| 8 | +SELECT icustay_id, |
| 9 | + linkorderid, |
| 10 | + MIN(starttime) AS starttime, |
| 11 | + MAX(endtime) AS endtime |
| 12 | +FROM inputevents_mv |
| 13 | +WHERE itemid = 225155 |
| 14 | + AND statusdescription != 'Rewritten' |
| 15 | +GROUP BY icustay_id, |
| 16 | + linkorderid |
| 17 | +ORDER BY icustay_id, |
| 18 | + starttime |
| 19 | +``` |
| 20 | + |
| 21 | +Result: |
| 22 | + |
| 23 | +| icustay_id | linkorderid | starttime | endtime | |
| 24 | +|------------|---------------|---------------------|---------------------| |
| 25 | +| `1111` | 11 | 05.08.2132 00:45 | 05.08.2132 02:10 | |
| 26 | +| 2222 | `22` | 27.03.2136 12:54 | 28.03.2136 `00:39`| |
| 27 | +| 2222 | `23` | 28.03.2136 `00:39`| 28.03.2136 12:37 | |
| 28 | +| 3333 | 33 | 15.06.2118 01:13 | 15.06.2118 13:00 | |
| 29 | +| 3333 | 34 | 15.06.2118 13:00 | 15.06.2118 `14:30`| |
| 30 | +| 3333 | 35 | 15.06.2118 `21:31`| 16.06.2118 09:29 | |
| 31 | + |
| 32 | +*Notably (see `highlights`), Octreotide is given one or multiple times, although `linkorderid` is different, with zero, one or more interruptions.* |
| 33 | + |
| 34 | +Therefore, add row number (`rn`) and indicator, whether duration continues or not (`to_prev`). Also, add enumeration based on that grouping (`gn`). |
| 35 | + |
| 36 | +```sql |
| 37 | +WITH t0 |
| 38 | + AS (SELECT icustay_id, |
| 39 | + Min(starttime) AS starttime, |
| 40 | + Max(endtime) AS endtime |
| 41 | + FROM inputevents_mv |
| 42 | + WHERE itemid = 225155 |
| 43 | + AND statusdescription != 'Rewritten' |
| 44 | + GROUP BY icustay_id, |
| 45 | + linkorderid), |
| 46 | + t1 |
| 47 | + AS (SELECT *, |
| 48 | + ROW_NUMBER() |
| 49 | + OVER ( |
| 50 | + partition BY icustay_id |
| 51 | + ORDER BY starttime) AS rn, |
| 52 | + ( CASE |
| 53 | + WHEN ( LAG(endtime) |
| 54 | + OVER ( |
| 55 | + partition BY icustay_id |
| 56 | + ORDER BY starttime) = starttime ) THEN 1 |
| 57 | + ELSE 0 |
| 58 | + END ) AS to_prev |
| 59 | + FROM t0) |
| 60 | +SELECT *, |
| 61 | + ROW_NUMBER() |
| 62 | + OVER ( |
| 63 | + partition BY icustay_id, to_prev |
| 64 | + ORDER BY starttime) AS gn |
| 65 | +FROM t1 |
| 66 | +ORDER BY icustay_id, |
| 67 | + starttime |
| 68 | +``` |
| 69 | + |
| 70 | +Result: |
| 71 | + |
| 72 | +| icustay_id | linkorderid | starttime | endtime | row | to_prev | gn | |
| 73 | +|------------|-------------|------------------|------------------|-----|---------|----| |
| 74 | +| 1111 | 11 | 05.08.2132 00:45 | 05.08.2132 02:10 | 1 | 0 | 1 | |
| 75 | +| 2222 | 22 | 27.03.2136 12:54 | 28.03.2136 00:39 | 1 | 0 | 1 | |
| 76 | +| 2222 | 23 | 28.03.2136 00:39 | 28.03.2136 12:37 | 2 | 1 | 2 | |
| 77 | +| 3333 | 33 | 15.06.2118 01:13 | 15.06.2118 13:00 | 1 | 0 | 1 | |
| 78 | +| 3333 | 34 | 15.06.2118 13:00 | 15.06.2118 14:30 | 2 | 1 | 1 | |
| 79 | +| 3333 | 35 | 15.06.2118 21:31 | 16.06.2118 09:29 | 3 | 0 | 2 | |
| 80 | + |
| 81 | +Now in case intake starts anew, set current group number. If not, return to previous group number (by `rn-gn`). Store result `group_id`. |
| 82 | + |
| 83 | +```sql |
| 84 | +WITH t0 |
| 85 | + AS (SELECT icustay_id, |
| 86 | + Min(starttime) AS starttime, |
| 87 | + Max(endtime) AS endtime |
| 88 | + FROM inputevents_mv |
| 89 | + WHERE itemid = 225155 |
| 90 | + AND statusdescription != 'Rewritten' |
| 91 | + GROUP BY icustay_id, |
| 92 | + linkorderid), |
| 93 | + t1 |
| 94 | + AS (SELECT *, |
| 95 | + ROW_NUMBER() |
| 96 | + OVER ( |
| 97 | + partition BY icustay_id |
| 98 | + ORDER BY starttime) AS rn, |
| 99 | + ( CASE |
| 100 | + WHEN ( LAG(endtime) |
| 101 | + OVER ( |
| 102 | + partition BY icustay_id |
| 103 | + ORDER BY starttime) = starttime ) THEN 1 |
| 104 | + ELSE 0 |
| 105 | + END ) AS to_prev |
| 106 | + FROM t0), |
| 107 | + t2 |
| 108 | + AS (SELECT *, |
| 109 | + ROW_NUMBER() |
| 110 | + OVER ( |
| 111 | + partition BY icustay_id, to_prev |
| 112 | + ORDER BY starttime) AS gn |
| 113 | + FROM t1) |
| 114 | +SELECT icustay_id, |
| 115 | + starttime, |
| 116 | + endtime, |
| 117 | + gn, |
| 118 | + ( CASE |
| 119 | + WHEN to_prev = 0 THEN gn |
| 120 | + ELSE ( rn - gn ) |
| 121 | + END ) AS groupid |
| 122 | +FROM t2 |
| 123 | +ORDER BY icustay_id, |
| 124 | + starttime |
| 125 | +``` |
| 126 | + |
| 127 | +Result: |
| 128 | + |
| 129 | +| icustay_id | linkorderid | starttime | endtime | row | to_prev | gn | group_id | |
| 130 | +|------------|-------------|------------------|------------------|-----|---------|----|----------| |
| 131 | +| 1111 | 11 | 05.08.2132 00:45 | 05.08.2132 02:10 | 1 | 0 | 1 | 1 | |
| 132 | +| 2222 | 22 | 27.03.2136 12:54 | 28.03.2136 00:39 | 1 | 0 | 1 | 1 | |
| 133 | +| 2222 | 23 | 28.03.2136 00:39 | 28.03.2136 12:37 | 2 | 1 | 2 | 1 | |
| 134 | +| 3333 | 33 | 15.06.2118 01:13 | 15.06.2118 13:00 | 1 | 0 | 1 | 1 | |
| 135 | +| 3333 | 34 | 15.06.2118 13:00 | 15.06.2118 14:30 | 2 | 1 | 1 | 1 | |
| 136 | +| 3333 | 35 | 15.06.2118 21:31 | 16.06.2118 09:29 | 3 | 0 | 2 | 2 | |
| 137 | + |
| 138 | +Now, group by `group_id`, clean up and provide `min(starttime)` and `max(endtime)` to calculate durations (`d_hours`). Also, count the number of intakes to be able to later extract the previous number of intakes (`octreo_num`). |
| 139 | + |
| 140 | +```sql |
| 141 | +WITH t0 |
| 142 | + AS (SELECT icustay_id, |
| 143 | + Min(starttime) AS starttime, |
| 144 | + Max(endtime) AS endtime |
| 145 | + FROM inputevents_mv |
| 146 | + WHERE itemid = 225155 |
| 147 | + AND statusdescription != 'Rewritten' |
| 148 | + GROUP BY icustay_id, |
| 149 | + linkorderid), |
| 150 | + t1 |
| 151 | + AS (SELECT *, |
| 152 | + ROW_NUMBER() |
| 153 | + OVER ( |
| 154 | + partition BY icustay_id |
| 155 | + ORDER BY starttime) AS rn, |
| 156 | + ( CASE |
| 157 | + WHEN ( LAG(endtime) |
| 158 | + OVER ( |
| 159 | + partition BY icustay_id |
| 160 | + ORDER BY starttime) = starttime ) THEN 1 |
| 161 | + ELSE 0 |
| 162 | + END ) AS to_prev |
| 163 | + FROM t0), |
| 164 | + t2 |
| 165 | + AS (SELECT *, |
| 166 | + ROW_NUMBER() |
| 167 | + OVER ( |
| 168 | + partition BY icustay_id, to_prev |
| 169 | + ORDER BY starttime) AS gn |
| 170 | + FROM t1), |
| 171 | + t3 |
| 172 | + AS (SELECT *, |
| 173 | + ( CASE |
| 174 | + WHEN to_prev = 0 THEN gn |
| 175 | + ELSE ( rn - gn ) |
| 176 | + END ) AS groupid |
| 177 | + FROM t2) |
| 178 | +SELECT icustay_id, |
| 179 | + groupid AS octreonum, |
| 180 | + Min(starttime) AS starttime, |
| 181 | + Max(endtime) AS endtime |
| 182 | +FROM t3 |
| 183 | +GROUP BY icustay_id, |
| 184 | + octreonum |
| 185 | +ORDER BY icustay_id, |
| 186 | + starttime |
| 187 | +``` |
| 188 | + |
| 189 | +Result: |
| 190 | + |
| 191 | +| icustay_id | octreo_num | d_hours | |
| 192 | +|------------|------------|---------| |
| 193 | +| 1111 | 1 | 01:25 | |
| 194 | +| 2222 | 1 | 23:43 | |
| 195 | +| 3333 | 1 | 13:17 | |
| 196 | +| 3333 | 2 | 11:58 | |
0 commit comments