Ctr

Sleep Science

You are a data scientist working on a fitness app similar to Apple Fitness called "Avocado Fitness". You were hired to do research on optimizing sleep quality using people's personal data.

Users' smart watches monitor their sleep metrics and upload them to our servers. That data is in the sleep_reports table.


sleep_reports
column_name type
id
INTEGER
user_id
INTEGER
ts
TIMESTAMPTZ
efficiency
INTEGER
rem_perc
INTEGER
duration_min
INTEGER

To start your research, you want to see if people's mood before their sleep affects their sleep quality. The app also allows users to self-report their mood throughout the day on a scale of 1-10. That data is in the mood_updates table.


mood_updates
column_name type
id
INTEGER
user_id
INTEGER
ts
TIMESTAMPTZ
mood
INTEGER

You plan to do some regression modeling to start examining the influence of mood on sleep quality. To do this, you want to select all users' sleep reports and join each 1 with that user's most recent mood update. For example, if a user has a sleep report submitted at 9am on Dec 10th, and they have mood updates at 11pm Dec 9th, 8am Dec 10th, and 3pm Dec 10th, you want to join that sleep report with the mood update at 8am Dec 10th. Do this for all sleep reports.

Select all the sleep report data plus the mood and ts from the relevant mood update. If there is no applicable mood update, set the mood to be NULL.

© 2022 Andrew Carlson. All rights reserved.

id
INTEGER
user_id
INTEGER
ts
TIMESTAMPTZ
efficiency
INTEGER
rem_perc
INTEGER
duration_min
INTEGER
mood
INTEGER
mood_update_ts
TIMESTAMPTZ
1
702204070
340679
2022-08-01 18:14:21+00
66
40
600
2
2022-08-01 17:20:53+00
2
923745649
340679
2022-08-02 16:26:52+00
96
27
334
5
2022-08-02 13:39:55+00
3
844301698
340679
2022-08-03 09:22:52+00
64
5
584
6
2022-08-02 17:08:18+00
4
147421681
340679
2022-08-04 08:20:27+00
97
29
439
4
2022-08-03 21:21:56+00
5
440574080
340679
2022-08-05 05:45:49+00
63
40
482
4
2022-08-03 21:21:56+00
6
423756723
340679
2022-08-06 03:20:11+00
95
36
319
5
2022-08-06 02:02:36+00
7
992550587
340679
2022-08-07 11:15:45+00
58
21
390
5
2022-08-07 01:18:51+00
8
176007078
340679
2022-08-08 03:30:40+00
91
30
464
5
2022-08-07 01:18:51+00
9
601278022
340679
2022-08-09 10:06:20+00
76
39
531
4
2022-08-08 20:43:33+00
10
517163908
340679
2022-08-10 01:32:03+00
50
40
541
4
2022-08-09 18:54:10+00
11
74438942
1153627
2022-08-01 06:00:07+00
48
38
493
NULLNULL
12
339853657
1153627
2022-08-02 09:50:31+00
51
8
569
9
2022-08-01 16:38:44+00
13
212965043
1153627
2022-08-03 13:54:05+00
51
30
466
8
2022-08-03 06:52:43+00
14
343014070
1153627
2022-08-04 04:41:23+00
78
34
578
8
2022-08-03 06:52:43+00
15
193293039
1153627
2022-08-05 13:55:10+00
62
7
515
3
2022-08-04 15:35:34+00
16
221984766
1153627
2022-08-06 01:01:05+00
86
12
572
3
2022-08-04 15:35:34+00
17
484736929
1153627
2022-08-07 15:56:26+00
48
30
599
3
2022-08-04 15:35:34+00
18
839058995
1153627
2022-08-08 15:33:28+00
60
23
335
3
2022-08-04 15:35:34+00
19
564840848
1153627
2022-08-09 00:43:09+00
56
28
535
3
2022-08-04 15:35:34+00
20
993113951
1153627
2022-08-10 18:50:15+00
62
30
379
3
2022-08-04 15:35:34+00
21
451737536
1587681
2022-08-01 03:48:06+00
82
24
505
NULLNULL
22
463040272
1587681
2022-08-02 19:09:41+00
80
31
413
8
2022-08-02 11:03:04+00
23
470570193
1587681
2022-08-03 19:46:35+00
60
27
334
8
2022-08-03 08:33:46+00
24
953151896
1587681
2022-08-04 17:54:31+00
84
35
552
4
2022-08-03 20:45:48+00
25
4945452
1587681
2022-08-05 22:01:03+00
65
33
369
9
2022-08-05 13:24:37+00
26
651124714
1587681
2022-08-06 02:10:21+00
63
31
573
6
2022-08-05 22:33:04+00
27
116190819
1587681
2022-08-07 19:31:25+00
98
5
558
10
2022-08-07 17:16:09+00
28
304180912
1587681
2022-08-08 01:10:05+00
86
25
531
10
2022-08-07 17:16:09+00
29
488402476
1587681
2022-08-09 00:17:39+00
63
27
574
2
2022-08-08 17:42:25+00
30
394766672
1587681
2022-08-10 20:24:11+00
87
38
491
8
2022-08-10 14:32:37+00
31
905335508
2126664
2022-08-01 04:21:16+00
61
18
579
NULLNULL
32
181767188
2126664
2022-08-02 08:33:55+00
85
10
368
1
2022-08-01 17:31:00+00
33
678985316
2126664
2022-08-03 10:26:42+00
71
26
497
10
2022-08-02 17:48:44+00
34
634391970
2126664
2022-08-04 11:01:56+00
64
20
408
2
2022-08-04 01:05:16+00
35
252789002
2126664
2022-08-05 09:27:19+00
73
24
454
4
2022-08-05 07:22:30+00
36
38249233
2126664
2022-08-06 21:41:07+00
55
16
429
1
2022-08-05 16:13:00+00
37
1622159
2126664
2022-08-07 18:23:04+00
44
10
312
8
2022-08-07 16:34:24+00
38
174502819
2126664
2022-08-08 17:33:24+00
65
33
301
5
2022-08-08 17:07:48+00
39
622911321
2126664
2022-08-09 03:11:02+00
61
9
479
5
2022-08-09 00:54:49+00
40
870420850
2126664
2022-08-10 19:19:57+00
98
38
581
8
2022-08-10 09:01:33+00
41
770932325
2354652
2022-08-01 11:55:13+00
49
25
600
NULLNULL
42
380851475
2354652
2022-08-02 07:15:55+00
56
23
561
2
2022-08-01 19:32:02+00
43
128134659
2354652
2022-08-03 21:55:14+00
65
33
369
3
2022-08-03 09:46:19+00
44
633737408
2354652
2022-08-04 13:58:12+00
64
36
547
1
2022-08-04 03:23:22+00
45
376863000
2354652
2022-08-05 16:37:29+00
90
34
306
6
2022-08-05 05:44:28+00
46
530619300
2354652
2022-08-06 20:04:47+00
71
14
540
6
2022-08-05 22:16:36+00
47
219570251
2354652
2022-08-07 04:34:19+00
75
10
360
6
2022-08-05 22:16:36+00
48
9496878
2354652
2022-08-08 13:15:27+00
87
11
547
6
2022-08-05 22:16:36+00
49
323882988
2354652
2022-08-09 16:36:05+00
50
29
433
5
2022-08-09 07:28:19+00
50
827113006
2354652
2022-08-10 22:00:26+00
78
31
471
6
2022-08-10 14:58:44+00
51
808365217
2620167
2022-08-01 22:27:36+00
75
19
446
3
2022-08-01 09:13:02+00
52
830239021
2620167
2022-08-02 12:50:36+00
96
28
389
3
2022-08-01 09:13:02+00
53
797272459
2620167
2022-08-03 23:57:57+00
70
9
435
10
2022-08-03 14:36:23+00
54
429981806
2620167
2022-08-04 21:54:14+00
97
33
592
4
2022-08-04 06:54:43+00
55
377797434
2620167
2022-08-05 14:11:42+00
66
34
582
7
2022-08-05 02:11:30+00
56
125322006
2620167
2022-08-06 07:04:22+00
92
36
595
7
2022-08-05 02:11:30+00
57
896310759
2620167
2022-08-07 05:22:22+00
96
15
552
7
2022-08-05 02:11:30+00
58
392584205
2620167
2022-08-08 05:24:35+00
55
13
429
8
2022-08-07 11:51:38+00
59
329294828
2620167
2022-08-09 07:58:43+00
73
25
531
4
2022-08-09 07:41:46+00
60
195053793
2620167
2022-08-10 20:31:26+00
42
39
361
10
2022-08-10 07:14:46+00
61
323516567
2620984
2022-08-01 10:54:42+00
99
6
497
NULLNULL
62
465625850
2620984
2022-08-02 13:59:47+00
86
21
388
5
2022-08-02 03:59:44+00
63
400877061
2620984
2022-08-03 04:38:04+00
43
8
480
5
2022-08-02 03:59:44+00
64
994706659
2620984
2022-08-04 03:15:46+00
63
17
305
5
2022-08-03 16:51:17+00
65
82350071
2620984
2022-08-05 07:04:59+00
53
15
308
2
2022-08-04 19:24:08+00
66
555342783
2620984
2022-08-06 16:58:49+00
90
28
539
10
2022-08-06 14:43:50+00
67
966997139
2620984
2022-08-07 13:28:46+00
79
21
530
7
2022-08-07 08:24:07+00
68
443420961
2620984
2022-08-08 16:20:07+00
66
31
328
8
2022-08-08 01:50:22+00
69
539793363
2620984
2022-08-09 03:54:12+00
68
34
555
8
2022-08-08 01:50:22+00
70
241732254
2620984
2022-08-10 20:30:50+00
82
37
449
3
2022-08-10 17:59:50+00
71
504231820
2705548
2022-08-01 17:53:12+00
85
18
376
NULLNULL
72
801910882
2705548
2022-08-02 17:20:44+00
73
25
439
NULLNULL
73
572764813
2705548
2022-08-03 17:24:01+00
94
11
561
2
2022-08-03 16:21:50+00
74
639100277
2705548
2022-08-04 22:02:40+00
69
34
429
7
2022-08-04 20:21:41+00
75
540717046
2705548
2022-08-05 21:15:56+00
73
31
494
1
2022-08-05 18:37:31+00
76
833239832
2705548
2022-08-06 12:00:48+00
67
40
316
1
2022-08-05 18:37:31+00
77
208242735
2705548
2022-08-07 17:13:13+00
77
31
480
1
2022-08-05 18:37:31+00
78
35128684
2705548
2022-08-08 15:53:18+00
49
39
517
1
2022-08-05 18:37:31+00
79
914603230
2705548
2022-08-09 20:48:30+00
52
18
561
6
2022-08-09 04:00:47+00
80
658044789
2705548
2022-08-10 18:56:34+00
89
23
438
6
2022-08-09 04:00:47+00
81
479698762
2781987
2022-08-01 14:10:14+00
40
27
419
NULLNULL
82
714195260
2781987
2022-08-02 19:30:33+00
42
5
324
8
2022-08-02 17:41:12+00
83
786632741
2781987
2022-08-03 19:20:08+00
82
38
474
2
2022-08-03 04:12:31+00
84
515606822
2781987
2022-08-04 08:54:59+00
55
32
300
2
2022-08-03 04:12:31+00
85
214759322
2781987
2022-08-05 03:47:11+00
69
36
380
7
2022-08-04 13:18:22+00
86
546626764
2781987
2022-08-06 21:23:04+00
48
33
498
4
2022-08-06 16:07:41+00
87
886642514
2781987
2022-08-07 02:12:39+00
69
11
317
4
2022-08-06 16:07:41+00
88
281799138
2781987
2022-08-08 22:05:07+00
40
27
498
2
2022-08-08 07:34:40+00
89
523206374
2781987
2022-08-09 20:14:23+00
78
14
323
7
2022-08-09 07:40:37+00
90
648837226
2781987
2022-08-10 05:46:26+00
74
27
478
1
2022-08-10 01:45:56+00
91
680558370
3075221
2022-08-01 02:51:41+00
63
27
582
NULLNULL
92
107817939
3075221
2022-08-02 12:52:57+00
51
13
320
NULLNULL
93
189978681
3075221
2022-08-03 07:30:26+00
56
21
332
NULLNULL
94
374020267
3075221
2022-08-04 16:58:03+00
54
25
430
4
2022-08-03 21:25:03+00
95
547572589
3075221
2022-08-05 13:08:01+00
90
5
341
4
2022-08-03 21:25:03+00
96
111326347
3075221
2022-08-06 17:00:18+00
85
21
485
7
2022-08-06 06:29:56+00
97
918872719
3075221
2022-08-07 13:45:13+00
82
28
368
5
2022-08-07 10:07:32+00
98
109869328
3075221
2022-08-08 08:38:15+00
66
38
349
5
2022-08-07 10:07:32+00
99
655805981
3075221
2022-08-09 02:51:00+00
68
8
333
1
2022-08-08 20:10:19+00
100
389101325
3075221
2022-08-10 17:43:18+00
60
11
338
1
2022-08-08 20:10:19+00
101
672634485
3210684
2022-08-01 00:02:48+00
66
22
464
NULLNULL
102
821623142
3210684
2022-08-02 15:33:25+00
51
34
499
10
2022-08-02 13:34:52+00
103
394986434
3210684
2022-08-03 13:14:24+00
71
16
302
10
2022-08-02 13:34:52+00
104
269557632
3210684
2022-08-04 15:15:12+00
90
15
422
4
2022-08-03 16:14:14+00
105
276731518
3210684
2022-08-05 13:34:40+00
66
6
343
3
2022-08-04 18:55:43+00
106
605392772
3210684
2022-08-06 23:26:59+00
42
21
503
9
2022-08-06 15:58:32+00
107
740237831
3210684
2022-08-07 20:39:49+00
82
9
455
7
2022-08-07 16:02:02+00
108
42704944
3210684
2022-08-08 12:31:14+00
95
39
472
5
2022-08-08 08:45:07+00
109
711584631
3210684
2022-08-09 10:00:55+00
64
10
431
9
2022-08-09 03:30:46+00
110
851163594
3210684
2022-08-10 10:12:52+00
80
16
385
6
2022-08-10 01:04:45+00
111
695090492
3350933
2022-08-01 05:31:18+00
51
12
545
NULLNULL
112
336656113
3350933
2022-08-02 22:43:50+00
68
36
529
4
2022-08-02 22:31:43+00
113
407512838
3350933
2022-08-03 12:18:50+00
68
36
594
9
2022-08-03 06:47:05+00
114
999044206
3350933
2022-08-04 03:27:49+00
55
35
350
10
2022-08-04 03:21:58+00
115
394767283
3350933
2022-08-05 01:58:56+00
99
33
520
5
2022-08-04 20:07:43+00
116
264697403
3350933
2022-08-06 05:08:50+00
60
17
408
5
2022-08-04 20:07:43+00
117
844864625
3350933
2022-08-07 09:19:12+00
69
22
530
8
2022-08-06 15:52:06+00
118
780943438
3350933
2022-08-08 13:57:26+00
45
25
497
9
2022-08-08 03:22:32+00
119
878953224
3350933
2022-08-09 18:47:48+00
85
23
398
1
2022-08-09 06:04:42+00
120
302302969
3350933
2022-08-10 22:41:28+00
85
10
350
4
2022-08-10 04:51:27+00
121
288784447
4593710
2022-08-01 10:07:32+00
75
20
523
NULLNULL
122
691871625
4593710
2022-08-02 02:53:35+00
73
40
410
NULLNULL
123
538137380
4593710
2022-08-03 09:16:43+00
77
14
318
NULLNULL
124
21464013
4593710
2022-08-04 19:46:01+00
80
14
407
NULLNULL
125
948054300
4593710
2022-08-05 13:03:59+00
67
13
378
NULLNULL
126
330870294
4593710
2022-08-06 13:23:38+00
84
19
576
NULLNULL
127
523837030
4593710
2022-08-07 15:09:09+00
54
40
560
NULLNULL
128
989504700
4593710
2022-08-08 23:08:51+00
55
20
309
NULLNULL
129
65269292
4593710
2022-08-09 14:19:48+00
45
10
535
NULLNULL
130
23397277
4593710
2022-08-10 00:35:49+00
52
28
566
NULLNULL
131
682750861
5184410
2022-08-01 23:15:48+00
80
5
302
NULLNULL
132
21456109
5184410
2022-08-02 23:01:25+00
62
16
319
NULLNULL
133
934314919
5184410
2022-08-03 14:46:27+00
56
19
372
7
2022-08-03 12:40:54+00
134
43976274
5184410
2022-08-04 12:24:09+00
40
23
351
7
2022-08-03 12:40:54+00
135
407948278
5184410
2022-08-05 09:49:00+00
89
26
460
1
2022-08-05 09:33:19+00
136
833742975
5184410
2022-08-06 11:40:41+00
58
22
358
5
2022-08-06 11:28:14+00
137
616433087
5184410
2022-08-07 02:13:57+00
47
37
469
9
2022-08-06 16:23:22+00
138
174443698
5184410
2022-08-08 11:53:18+00
72
15
528
1
2022-08-07 17:55:08+00
139
264826050
5184410
2022-08-09 12:25:32+00
40
28
381
5
2022-08-09 09:17:26+00
140
919358966
5184410
2022-08-10 11:38:22+00
74
39
558
5
2022-08-09 09:17:26+00
141
553842577
5431264
2022-08-01 23:58:41+00
72
20
546
10
2022-08-01 21:36:15+00
142
653526611
5431264
2022-08-02 20:55:50+00
86
26
545
10
2022-08-01 21:36:15+00
143
824252535
5431264
2022-08-03 08:42:51+00
78
22
515
10
2022-08-01 21:36:15+00
144
789080938
5431264
2022-08-04 23:11:13+00
52
13
503
10
2022-08-01 21:36:15+00
145
753629895
5431264
2022-08-05 11:50:49+00
75
5
496
10
2022-08-01 21:36:15+00
146
962369721
5431264
2022-08-06 13:13:42+00
69
35
548
10
2022-08-01 21:36:15+00
147
982377270
5431264
2022-08-07 14:44:44+00
64
8
487
10
2022-08-01 21:36:15+00
148
362296116
5431264
2022-08-08 13:31:02+00
91
22
502
10
2022-08-01 21:36:15+00
149
68950793
5431264
2022-08-09 22:14:55+00
59
19
539
1
2022-08-09 13:59:19+00
150
492863521
5431264
2022-08-10 22:50:01+00
44
29
548
1
2022-08-09 13:59:19+00
151
729759967
5605737
2022-08-01 18:50:25+00
62
15
433
NULLNULL
152
66065556
5605737
2022-08-02 07:29:45+00
77
19
408
NULLNULL
153
212103210
5605737
2022-08-03 21:24:24+00
57
26
467
1
2022-08-03 03:43:30+00
154
895817239
5605737
2022-08-04 03:13:24+00
47
5
311
1
2022-08-03 03:43:30+00
155
863340207
5605737
2022-08-05 18:14:03+00
66
30
394
8
2022-08-05 01:43:24+00
156
931607637
5605737
2022-08-06 23:07:06+00
75
22
554
9
2022-08-06 14:42:22+00
157
784991213
5605737
2022-08-07 10:17:38+00
57
16
346
10
2022-08-07 09:22:25+00
158
431482034
5605737
2022-08-08 03:28:17+00
80
39
511
6
2022-08-07 22:15:14+00
159
851703570
5605737
2022-08-09 13:22:23+00
88
24
566
8
2022-08-09 09:41:17+00
160
516559391
5605737
2022-08-10 12:23:00+00
43
6
565
4
2022-08-10 01:11:29+00
161
555887845
5921538
2022-08-01 16:51:44+00
75
34
422
NULLNULL
162
244026228
5921538
2022-08-02 22:53:01+00
64
17
506
NULLNULL
163
903204882
5921538
2022-08-03 08:49:18+00
44
8
339
6
2022-08-03 01:32:54+00
164
912297735
5921538
2022-08-04 11:36:16+00
47
28
478
1
2022-08-03 14:11:55+00
165
888907458
5921538
2022-08-05 12:25:56+00
84
11
575
1
2022-08-03 14:11:55+00
166
342502349
5921538
2022-08-06 22:49:07+00
92
33
591
9
2022-08-06 01:48:36+00
167
745521889
5921538
2022-08-07 21:26:52+00
44
27
378
8
2022-08-07 14:52:18+00
168
305260337
5921538
2022-08-08 00:37:39+00
49
32
578
8
2022-08-07 14:52:18+00
169
973142422
5921538
2022-08-09 05:07:21+00
78
21
555
4
2022-08-09 00:47:52+00
170
373363406
5921538
2022-08-10 20:23:42+00
65
38
362
1
2022-08-10 19:50:04+00
171
83427706
7328413
2022-08-01 18:06:51+00
82
38
491
8
2022-08-01 10:10:07+00
172
293473249
7328413
2022-08-02 01:38:14+00
67
9
491
8
2022-08-01 10:10:07+00
173
776250642
7328413
2022-08-03 14:02:22+00
78
16
577
3
2022-08-03 09:08:54+00
174
903889723
7328413
2022-08-04 22:45:06+00
83
29
366
7
2022-08-04 07:21:08+00
175
359182387
7328413
2022-08-05 21:11:49+00
76
20
452
7
2022-08-04 07:21:08+00
176
757596948
7328413
2022-08-06 12:36:23+00
40
22
352
10
2022-08-06 11:34:14+00
177
932968474
7328413
2022-08-07 15:16:01+00
90
33
581
10
2022-08-06 11:34:14+00
178
132932942
7328413
2022-08-08 22:17:45+00
79
11
470
9
2022-08-08 01:59:37+00
179
16858823
7328413
2022-08-09 07:29:07+00
52
17
391
9
2022-08-08 01:59:37+00
180
746923347
7328413
2022-08-10 10:36:45+00
67
30
425
9
2022-08-08 01:59:37+00
181
489327130
8023891
2022-08-01 07:55:17+00
47
15
303
NULLNULL
182
382796491
8023891
2022-08-02 01:10:50+00
63
38
592
NULLNULL
183
76111938
8023891
2022-08-03 12:18:14+00
50
33
337
NULLNULL
184
301837792
8023891
2022-08-04 17:00:12+00
99
28
508
NULLNULL
185
512945464
8023891
2022-08-05 20:25:28+00
85
14
446
NULLNULL
186
690456250
8023891
2022-08-06 18:37:54+00
60
30
451
NULLNULL
187
116595973
8023891
2022-08-07 18:20:01+00
62
21
439
NULLNULL
188
490121620
8023891
2022-08-08 06:21:17+00
64
39
571
NULLNULL
189
395091662
8023891
2022-08-09 22:42:34+00
97
9
423
NULLNULL
190
420943609
8023891
2022-08-10 20:23:39+00
88
14
558
NULLNULL
191
689159843
9760877
2022-08-01 09:11:21+00
55
19
427
8
2022-08-01 07:48:23+00
192
704166890
9760877
2022-08-02 11:23:15+00
53
30
460
8
2022-08-02 10:09:21+00
193
390080298
9760877
2022-08-03 18:07:53+00
76
12
581
6
2022-08-03 10:09:35+00
194
152850390
9760877
2022-08-04 15:55:43+00
61
8
421
8
2022-08-04 14:55:35+00
195
580505814
9760877
2022-08-05 23:15:24+00
63
6
448
7
2022-08-05 15:56:05+00
196
232863092
9760877
2022-08-06 23:13:18+00
49
9
316
8
2022-08-06 23:06:47+00
197
625918566
9760877
2022-08-07 03:58:50+00
65
30
456
8
2022-08-06 23:06:47+00
198
615256031
9760877
2022-08-08 23:52:09+00
70
33
342
5
2022-08-08 21:33:52+00
199
623653871
9760877
2022-08-09 14:40:51+00
41
9
492
5
2022-08-09 12:57:12+00
200
487832200
9760877
2022-08-10 20:36:40+00
77
28
598
9
2022-08-10 17:45:18+00
200 rows

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