Ctr

Realtime ER Visits

You are a software engineer working in logistics for a hypothetical hospital in Iceland. You are responsible for software that determines optimal staffing. Your task is to help ensure that the emergency room (ER) remains adequately staffed as patient load fluctuates throughout the day. To do this, you need to look at patient volume throughout the day in realtime. Realtime data for number of visits is in the realtime_er_visits table.


realtime_er_visits
column_name type
datetime_t
TIMESTAMPTZ
visit_count
INTEGER

The datetime_t column represents a 15-minute interval beginning at that time. The visit_count is the number of people that came into the ER during that interval.

To help the logistics team, your software needs to display a moving average, a.k.a. "rolling mean", of the number of people that visit the ER for each time interval. In our case, the moving average should be for 2 hours. So, the moving average for the 11:30:00 - 11:44:59 interval should go back to 9:45:00.

For each time interval in the realtime_er_visits table, select the datetime, the number of new visitors that interval, and the moving average for the past 2 hours (name this column avg_visit_count).

2022-09-15T14:07:18.437261 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ 00:00 01-Aug 2022 00:00 02-Aug 00:00 03-Aug 12:00 12:00 12:00 Timestamp 0 20 40 60 80 100 No. ER Visits Realtime ER Visits avg_visit_count visit_count

This hospital has some 15-minute intervals where nobody comes into the ER. Interestingly, when these happen, there is no row for that time interval in the table instead of a 0. Your query will need to account for this, as the moving average for that time will probably not be 0. For a hint, notice that there is a Postgres function generate_series which works for TIMESTAMPTZs.

© 2022 Andrew Carlson. All rights reserved.

datetime_t
TIMESTAMPTZ
visit_count
INTEGER
avg_visit_count
NUMERIC
1
2022-08-01 00:00:00+00
11
11.0000000000000000
2
2022-08-01 00:15:00+00
34
22.5000000000000000
3
2022-08-01 00:30:00+00
9
18.0000000000000000
4
2022-08-01 00:45:00+00
42
24.0000000000000000
5
2022-08-01 01:00:00+00
28
24.8000000000000000
6
2022-08-01 01:15:00+00
14
23.0000000000000000
7
2022-08-01 01:30:00+00
14
21.7142857142857143
8
2022-08-01 01:45:00+00
23
21.8750000000000000
9
2022-08-01 02:00:00+00
19
22.8750000000000000
10
2022-08-01 02:15:00+00
30
22.3750000000000000
11
2022-08-01 02:30:00+00
35
25.6250000000000000
12
2022-08-01 02:45:00+00
13
22.0000000000000000
13
2022-08-01 03:00:00+00
18
20.7500000000000000
14
2022-08-01 03:15:00+00
20
21.5000000000000000
15
2022-08-01 03:30:00+00
28
23.2500000000000000
16
2022-08-01 03:45:00+00
19
22.7500000000000000
17
2022-08-01 04:00:00+00
24
23.3750000000000000
18
2022-08-01 04:15:00+00
0
19.6250000000000000
19
2022-08-01 04:30:00+00
3
15.6250000000000000
20
2022-08-01 04:45:00+00
1
14.1250000000000000
21
2022-08-01 05:00:00+00
7
12.7500000000000000
22
2022-08-01 05:15:00+00
0
10.2500000000000000
23
2022-08-01 05:30:00+00
24
9.7500000000000000
24
2022-08-01 05:45:00+00
1
7.5000000000000000
25
2022-08-01 06:00:00+00
0
4.5000000000000000
26
2022-08-01 06:15:00+00
27
7.8750000000000000
27
2022-08-01 06:30:00+00
23
10.3750000000000000
28
2022-08-01 06:45:00+00
37
14.8750000000000000
29
2022-08-01 07:00:00+00
35
18.3750000000000000
30
2022-08-01 07:15:00+00
21
21.0000000000000000
31
2022-08-01 07:30:00+00
51
24.3750000000000000
32
2022-08-01 07:45:00+00
35
28.6250000000000000
33
2022-08-01 08:00:00+00
42
33.8750000000000000
34
2022-08-01 08:15:00+00
35
34.8750000000000000
35
2022-08-01 08:30:00+00
38
36.7500000000000000
36
2022-08-01 08:45:00+00
64
40.1250000000000000
37
2022-08-01 09:00:00+00
42
41.0000000000000000
38
2022-08-01 09:15:00+00
52
44.8750000000000000
39
2022-08-01 09:30:00+00
34
42.7500000000000000
40
2022-08-01 09:45:00+00
38
43.1250000000000000
41
2022-08-01 10:00:00+00
51
44.2500000000000000
42
2022-08-01 10:15:00+00
70
48.6250000000000000
43
2022-08-01 10:30:00+00
52
50.3750000000000000
44
2022-08-01 10:45:00+00
83
52.7500000000000000
45
2022-08-01 11:00:00+00
78
57.2500000000000000
46
2022-08-01 11:15:00+00
87
61.6250000000000000
47
2022-08-01 11:30:00+00
99
69.7500000000000000
48
2022-08-01 11:45:00+00
76
74.5000000000000000
49
2022-08-01 12:00:00+00
83
78.5000000000000000
50
2022-08-01 12:15:00+00
89
80.8750000000000000
51
2022-08-01 12:30:00+00
81
84.5000000000000000
52
2022-08-01 12:45:00+00
86
84.8750000000000000
53
2022-08-01 13:00:00+00
93
86.7500000000000000
54
2022-08-01 13:15:00+00
85
86.5000000000000000
55
2022-08-01 13:30:00+00
97
86.2500000000000000
56
2022-08-01 13:45:00+00
72
85.7500000000000000
57
2022-08-01 14:00:00+00
62
83.1250000000000000
58
2022-08-01 14:15:00+00
81
82.1250000000000000
59
2022-08-01 14:30:00+00
83
82.3750000000000000
60
2022-08-01 14:45:00+00
77
81.2500000000000000
61
2022-08-01 15:00:00+00
62
77.3750000000000000
62
2022-08-01 15:15:00+00
85
77.3750000000000000
63
2022-08-01 15:30:00+00
64
73.2500000000000000
64
2022-08-01 15:45:00+00
84
74.7500000000000000
65
2022-08-01 16:00:00+00
66
75.2500000000000000
66
2022-08-01 16:15:00+00
78
74.8750000000000000
67
2022-08-01 16:30:00+00
64
72.5000000000000000
68
2022-08-01 16:45:00+00
71
71.7500000000000000
69
2022-08-01 17:00:00+00
51
70.3750000000000000
70
2022-08-01 17:15:00+00
73
68.8750000000000000
71
2022-08-01 17:30:00+00
84
71.3750000000000000
72
2022-08-01 17:45:00+00
77
70.5000000000000000
73
2022-08-01 18:00:00+00
63
70.1250000000000000
74
2022-08-01 18:15:00+00
81
70.5000000000000000
75
2022-08-01 18:30:00+00
62
70.2500000000000000
76
2022-08-01 18:45:00+00
50
67.6250000000000000
77
2022-08-01 19:00:00+00
56
68.2500000000000000
78
2022-08-01 19:15:00+00
73
68.2500000000000000
79
2022-08-01 19:30:00+00
51
64.1250000000000000
80
2022-08-01 19:45:00+00
46
60.2500000000000000
81
2022-08-01 20:00:00+00
65
60.5000000000000000
82
2022-08-01 20:15:00+00
52
56.8750000000000000
83
2022-08-01 20:30:00+00
70
57.8750000000000000
84
2022-08-01 20:45:00+00
48
57.6250000000000000
85
2022-08-01 21:00:00+00
41
55.7500000000000000
86
2022-08-01 21:15:00+00
42
51.8750000000000000
87
2022-08-01 21:30:00+00
51
51.8750000000000000
88
2022-08-01 21:45:00+00
37
50.7500000000000000
89
2022-08-01 22:00:00+00
48
48.6250000000000000
90
2022-08-01 22:15:00+00
30
45.8750000000000000
91
2022-08-01 22:30:00+00
53
43.7500000000000000
92
2022-08-01 22:45:00+00
36
42.2500000000000000
93
2022-08-01 23:00:00+00
32
41.1250000000000000
94
2022-08-01 23:15:00+00
42
41.1250000000000000
95
2022-08-01 23:30:00+00
24
37.7500000000000000
96
2022-08-01 23:45:00+00
25
36.2500000000000000
97
2022-08-02 00:00:00+00
18
32.5000000000000000
98
2022-08-02 00:15:00+00
45
34.3750000000000000
99
2022-08-02 00:30:00+00
32
31.7500000000000000
100
2022-08-02 00:45:00+00
32
31.2500000000000000
101
2022-08-02 01:00:00+00
16
29.2500000000000000
102
2022-08-02 01:15:00+00
8
25.0000000000000000
103
2022-08-02 01:30:00+00
19
24.3750000000000000
104
2022-08-02 01:45:00+00
14
23.0000000000000000
105
2022-08-02 02:00:00+00
21
23.3750000000000000
106
2022-08-02 02:15:00+00
32
21.7500000000000000
107
2022-08-02 02:30:00+00
10
19.0000000000000000
108
2022-08-02 02:45:00+00
27
18.3750000000000000
109
2022-08-02 03:00:00+00
19
18.7500000000000000
110
2022-08-02 03:15:00+00
12
19.2500000000000000
111
2022-08-02 03:30:00+00
18
19.1250000000000000
112
2022-08-02 03:45:00+00
19
19.7500000000000000
113
2022-08-02 04:00:00+00
19
19.5000000000000000
114
2022-08-02 04:15:00+00
0
15.5000000000000000
115
2022-08-02 04:30:00+00
0
14.2500000000000000
116
2022-08-02 04:45:00+00
18
13.1250000000000000
117
2022-08-02 05:00:00+00
0
10.7500000000000000
118
2022-08-02 05:15:00+00
15
11.1250000000000000
119
2022-08-02 05:30:00+00
2
9.1250000000000000
120
2022-08-02 05:45:00+00
16
8.7500000000000000
121
2022-08-02 06:00:00+00
34
10.6250000000000000
122
2022-08-02 06:15:00+00
8
11.6250000000000000
123
2022-08-02 06:30:00+00
11
13.0000000000000000
124
2022-08-02 06:45:00+00
44
16.2500000000000000
125
2022-08-02 07:00:00+00
12
17.7500000000000000
126
2022-08-02 07:15:00+00
32
19.8750000000000000
127
2022-08-02 07:30:00+00
22
22.3750000000000000
128
2022-08-02 07:45:00+00
29
24.0000000000000000
129
2022-08-02 08:00:00+00
38
24.5000000000000000
130
2022-08-02 08:15:00+00
50
29.7500000000000000
131
2022-08-02 08:30:00+00
64
36.3750000000000000
132
2022-08-02 08:45:00+00
63
38.7500000000000000
133
2022-08-02 09:00:00+00
52
43.7500000000000000
134
2022-08-02 09:15:00+00
49
45.8750000000000000
135
2022-08-02 09:30:00+00
67
51.5000000000000000
136
2022-08-02 09:45:00+00
32
51.8750000000000000
137
2022-08-02 10:00:00+00
57
54.2500000000000000
138
2022-08-02 10:15:00+00
61
55.6250000000000000
139
2022-08-02 10:30:00+00
82
57.8750000000000000
140
2022-08-02 10:45:00+00
78
59.7500000000000000
141
2022-08-02 11:00:00+00
74
62.5000000000000000
142
2022-08-02 11:15:00+00
72
65.3750000000000000
143
2022-08-02 11:30:00+00
88
68.0000000000000000
144
2022-08-02 11:45:00+00
92
75.5000000000000000
145
2022-08-02 12:00:00+00
76
77.8750000000000000
146
2022-08-02 12:15:00+00
94
82.0000000000000000
147
2022-08-02 12:30:00+00
72
80.7500000000000000
148
2022-08-02 12:45:00+00
89
82.1250000000000000
149
2022-08-02 13:00:00+00
77
82.5000000000000000
150
2022-08-02 13:15:00+00
90
84.7500000000000000
151
2022-08-02 13:30:00+00
89
84.8750000000000000
152
2022-08-02 13:45:00+00
99
85.7500000000000000
153
2022-08-02 14:00:00+00
58
83.5000000000000000
154
2022-08-02 14:15:00+00
79
81.6250000000000000
155
2022-08-02 14:30:00+00
89
83.7500000000000000
156
2022-08-02 14:45:00+00
100
85.1250000000000000
157
2022-08-02 15:00:00+00
76
85.0000000000000000
158
2022-08-02 15:15:00+00
69
82.3750000000000000
159
2022-08-02 15:30:00+00
88
82.2500000000000000
160
2022-08-02 15:45:00+00
74
79.1250000000000000
161
2022-08-02 16:00:00+00
76
81.3750000000000000
162
2022-08-02 16:15:00+00
94
83.2500000000000000
163
2022-08-02 16:30:00+00
76
81.6250000000000000
164
2022-08-02 16:45:00+00
72
78.1250000000000000
165
2022-08-02 17:00:00+00
64
76.6250000000000000
166
2022-08-02 17:15:00+00
46
73.7500000000000000
167
2022-08-02 17:30:00+00
50
69.0000000000000000
168
2022-08-02 17:45:00+00
81
69.8750000000000000
169
2022-08-02 18:00:00+00
85
71.0000000000000000
170
2022-08-02 18:15:00+00
83
69.6250000000000000
171
2022-08-02 18:30:00+00
64
68.1250000000000000
172
2022-08-02 18:45:00+00
78
68.8750000000000000
173
2022-08-02 19:00:00+00
52
67.3750000000000000
174
2022-08-02 19:15:00+00
64
69.6250000000000000
175
2022-08-02 19:30:00+00
28
66.8750000000000000
176
2022-08-02 19:45:00+00
54
63.5000000000000000
177
2022-08-02 20:00:00+00
51
59.2500000000000000
178
2022-08-02 20:15:00+00
52
55.3750000000000000
179
2022-08-02 20:30:00+00
61
55.0000000000000000
180
2022-08-02 20:45:00+00
38
50.0000000000000000
181
2022-08-02 21:00:00+00
36
48.0000000000000000
182
2022-08-02 21:15:00+00
43
45.3750000000000000
183
2022-08-02 21:30:00+00
39
46.7500000000000000
184
2022-08-02 21:45:00+00
50
46.2500000000000000
185
2022-08-02 22:00:00+00
34
44.1250000000000000
186
2022-08-02 22:15:00+00
19
40.0000000000000000
187
2022-08-02 22:30:00+00
67
40.7500000000000000
188
2022-08-02 22:45:00+00
36
40.5000000000000000
189
2022-08-02 23:00:00+00
29
39.6250000000000000
190
2022-08-02 23:15:00+00
34
38.5000000000000000
191
2022-08-02 23:30:00+00
52
40.1250000000000000
192
2022-08-02 23:45:00+00
25
37.0000000000000000
193
2022-08-03 00:00:00+00
15
34.6250000000000000
194
2022-08-03 00:15:00+00
44
37.7500000000000000
195
2022-08-03 00:30:00+00
20
31.8750000000000000
196
2022-08-03 00:45:00+00
23
30.2500000000000000
197
2022-08-03 01:00:00+00
45
32.2500000000000000
198
2022-08-03 01:15:00+00
30
31.7500000000000000
199
2022-08-03 01:30:00+00
20
27.7500000000000000
200
2022-08-03 01:45:00+00
13
26.2500000000000000
201
2022-08-03 02:00:00+00
29
28.0000000000000000
202
2022-08-03 02:15:00+00
30
26.2500000000000000
203
2022-08-03 02:30:00+00
15
25.6250000000000000
204
2022-08-03 02:45:00+00
25
25.8750000000000000
205
2022-08-03 03:00:00+00
0
20.2500000000000000
206
2022-08-03 03:15:00+00
28
20.0000000000000000
207
2022-08-03 03:30:00+00
22
20.2500000000000000
208
2022-08-03 03:45:00+00
14
20.3750000000000000
209
2022-08-03 04:00:00+00
8
17.7500000000000000
210
2022-08-03 04:15:00+00
19
16.3750000000000000
211
2022-08-03 04:30:00+00
14
16.2500000000000000
212
2022-08-03 04:45:00+00
6
13.8750000000000000
213
2022-08-03 05:00:00+00
10
15.1250000000000000
214
2022-08-03 05:15:00+00
11
13.0000000000000000
215
2022-08-03 05:30:00+00
15
12.1250000000000000
216
2022-08-03 05:45:00+00
17
12.5000000000000000
217
2022-08-03 06:00:00+00
0
11.5000000000000000
218
2022-08-03 06:15:00+00
0
9.1250000000000000
219
2022-08-03 06:30:00+00
20
9.8750000000000000
220
2022-08-03 06:45:00+00
28
12.6250000000000000
221
2022-08-03 07:00:00+00
33
15.5000000000000000
222
2022-08-03 07:15:00+00
35
18.5000000000000000
223
2022-08-03 07:30:00+00
46
22.3750000000000000
224
2022-08-03 07:45:00+00
52
26.7500000000000000
225
2022-08-03 08:00:00+00
30
30.5000000000000000
226
2022-08-03 08:15:00+00
40
35.5000000000000000
227
2022-08-03 08:30:00+00
50
39.2500000000000000
228
2022-08-03 08:45:00+00
52
42.2500000000000000
229
2022-08-03 09:00:00+00
43
43.5000000000000000
230
2022-08-03 09:15:00+00
76
48.6250000000000000
231
2022-08-03 09:30:00+00
34
47.1250000000000000
232
2022-08-03 09:45:00+00
56
47.6250000000000000
233
2022-08-03 10:00:00+00
56
50.8750000000000000
234
2022-08-03 10:15:00+00
67
54.2500000000000000
235
2022-08-03 10:30:00+00
63
55.8750000000000000
236
2022-08-03 10:45:00+00
82
59.6250000000000000
237
2022-08-03 11:00:00+00
67
62.6250000000000000
238
2022-08-03 11:15:00+00
71
62.0000000000000000
239
2022-08-03 11:30:00+00
73
66.8750000000000000
240
2022-08-03 11:45:00+00
72
68.8750000000000000
241
2022-08-03 12:00:00+00
79
71.7500000000000000
242
2022-08-03 12:15:00+00
88
74.3750000000000000
243
2022-08-03 12:30:00+00
98
78.7500000000000000
244
2022-08-03 12:45:00+00
84
79.0000000000000000
245
2022-08-03 13:00:00+00
74
79.8750000000000000
246
2022-08-03 13:15:00+00
72
80.0000000000000000
247
2022-08-03 13:30:00+00
78
80.6250000000000000
248
2022-08-03 13:45:00+00
100
84.1250000000000000
249
2022-08-03 14:00:00+00
89
85.3750000000000000
250
2022-08-03 14:15:00+00
80
84.3750000000000000
251
2022-08-03 14:30:00+00
63
80.0000000000000000
252
2022-08-03 14:45:00+00
76
79.0000000000000000
253
2022-08-03 15:00:00+00
70
78.5000000000000000
254
2022-08-03 15:15:00+00
99
81.8750000000000000
255
2022-08-03 15:30:00+00
83
82.5000000000000000
256
2022-08-03 15:45:00+00
68
78.5000000000000000
257
2022-08-03 16:00:00+00
70
76.1250000000000000
258
2022-08-03 16:15:00+00
71
75.0000000000000000
259
2022-08-03 16:30:00+00
80
77.1250000000000000
260
2022-08-03 16:45:00+00
67
76.0000000000000000
261
2022-08-03 17:00:00+00
77
76.8750000000000000
262
2022-08-03 17:15:00+00
83
74.8750000000000000
263
2022-08-03 17:30:00+00
72
73.5000000000000000
264
2022-08-03 17:45:00+00
68
73.5000000000000000
265
2022-08-03 18:00:00+00
77
74.3750000000000000
266
2022-08-03 18:15:00+00
57
72.6250000000000000
267
2022-08-03 18:30:00+00
54
69.3750000000000000
268
2022-08-03 18:45:00+00
71
69.8750000000000000
269
2022-08-03 19:00:00+00
33
64.3750000000000000
270
2022-08-03 19:15:00+00
61
61.6250000000000000
271
2022-08-03 19:30:00+00
30
56.3750000000000000
272
2022-08-03 19:45:00+00
48
53.8750000000000000
273
2022-08-03 20:00:00+00
57
51.3750000000000000
274
2022-08-03 20:15:00+00
24
47.2500000000000000
275
2022-08-03 20:30:00+00
26
43.7500000000000000
276
2022-08-03 20:45:00+00
45
40.5000000000000000
277
2022-08-03 21:00:00+00
43
41.7500000000000000
278
2022-08-03 21:15:00+00
40
39.1250000000000000
279
2022-08-03 21:30:00+00
54
42.1250000000000000
280
2022-08-03 21:45:00+00
40
41.1250000000000000
281
2022-08-03 22:00:00+00
40
39.0000000000000000
282
2022-08-03 22:15:00+00
45
41.6250000000000000
283
2022-08-03 22:30:00+00
38
43.1250000000000000
284
2022-08-03 22:45:00+00
29
41.1250000000000000
285
2022-08-03 23:00:00+00
30
39.5000000000000000
286
2022-08-03 23:15:00+00
47
40.3750000000000000
287
2022-08-03 23:30:00+00
23
36.5000000000000000
288
2022-08-03 23:45:00+00
22
34.2500000000000000
288 rows

You haven't solved this challenge yet!
Are you sure you want to reveal the answer?