runtime
96 / 139

task Correlated subqueries · hard

Last known daily maximum

Find the Philadelphia station USW00094732 and calendar dates 2024-11-10 to 2024-11-25. Return date, tmax_c (NULL when not reported) and last_known_tmax: the tmax_c of the latest observation on or before that date.

what to return

Columns
datetmax_clast_known_tmax in this order
Rows
16 rows · any order
Skill
Carry the latest known value onto days with no observation. Refresher: CROSS JOIN & a complete date grid →

expected result

expected result16 rows · any order
datetmax_clast_known_tmax
2024-11-101515
2024-11-1123.323.3
2024-11-1214.414.4

first 3 of 16 rows

data you can query

calendar10 columns · 1,096 rows
datedate · unique idyearnumbermonthnumberday_of_yearnumberday_of_weeknumbermeteorological_seasontextday_of_monthnumberday_nametextis_weekendnumberquarternumber
2022-01-012022116winter1Saturday11
2022-01-022022120winter2Sunday11
2022-01-032022131winter3Monday01
2022-01-042022142winter4Tuesday01
2022-01-052022153winter5Wednesday01
2022-01-062022164winter6Thursday01
2022-01-072022175winter7Friday01
2022-01-082022186winter8Saturday11
2022-01-092022190winter9Sunday11
2022-01-1020221101winter10Monday01
datedate · unique id
2022-01-01
yearnumber
2022
monthnumber
1
day_of_yearnumber
1
day_of_weeknumber
6
meteorological_seasontext
winter
day_of_monthnumber
1
day_nametext
Saturday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-02
yearnumber
2022
monthnumber
1
day_of_yearnumber
2
day_of_weeknumber
0
meteorological_seasontext
winter
day_of_monthnumber
2
day_nametext
Sunday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-03
yearnumber
2022
monthnumber
1
day_of_yearnumber
3
day_of_weeknumber
1
meteorological_seasontext
winter
day_of_monthnumber
3
day_nametext
Monday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-04
yearnumber
2022
monthnumber
1
day_of_yearnumber
4
day_of_weeknumber
2
meteorological_seasontext
winter
day_of_monthnumber
4
day_nametext
Tuesday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-05
yearnumber
2022
monthnumber
1
day_of_yearnumber
5
day_of_weeknumber
3
meteorological_seasontext
winter
day_of_monthnumber
5
day_nametext
Wednesday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-06
yearnumber
2022
monthnumber
1
day_of_yearnumber
6
day_of_weeknumber
4
meteorological_seasontext
winter
day_of_monthnumber
6
day_nametext
Thursday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-07
yearnumber
2022
monthnumber
1
day_of_yearnumber
7
day_of_weeknumber
5
meteorological_seasontext
winter
day_of_monthnumber
7
day_nametext
Friday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-08
yearnumber
2022
monthnumber
1
day_of_yearnumber
8
day_of_weeknumber
6
meteorological_seasontext
winter
day_of_monthnumber
8
day_nametext
Saturday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-09
yearnumber
2022
monthnumber
1
day_of_yearnumber
9
day_of_weeknumber
0
meteorological_seasontext
winter
day_of_monthnumber
9
day_nametext
Sunday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-10
yearnumber
2022
monthnumber
1
day_of_yearnumber
10
day_of_weeknumber
1
meteorological_seasontext
winter
day_of_monthnumber
10
day_nametext
Monday
is_weekendnumber
0
quarternumber
1

first 10 of 1,096 rows · your queries use all of them

daily_weather10 columns · 109,214 rows
station_idtext · links to stationsobservation_datedate · links to calendartmax_cdecimaltmin_cdecimalprecipitation_mmdecimalprecip_tracenumbersnowfall_mmdecimalsnow_depth_mmdecimalavg_wind_speed_msdecimalpeak_gust_msdecimal
USW000031312022-01-0117.27.200NULLNULL26.3
USW000031312022-01-0220500NULLNULL1.15.4
USW000031312022-01-0317.83.900NULLNULL1.14.5
USW000031312022-01-0417.24.400NULLNULL1.44.5
USW000031312022-01-0521.15.600NULLNULL0.94
USW000031312022-01-0622.27.200NULLNULL0.63.6
USW000031312022-01-0716.17.200NULLNULL1.84.5
USW000031312022-01-0817.28.300NULLNULL1.54.5
USW000031312022-01-09256.700NULLNULL14.5
USW000031312022-01-1023.37.800NULLNULL1.44
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-01
tmax_cdecimal
17.2
tmin_cdecimal
7.2
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
2
peak_gust_msdecimal
6.3
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-02
tmax_cdecimal
20
tmin_cdecimal
5
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.1
peak_gust_msdecimal
5.4
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-03
tmax_cdecimal
17.8
tmin_cdecimal
3.9
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.1
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-04
tmax_cdecimal
17.2
tmin_cdecimal
4.4
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.4
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-05
tmax_cdecimal
21.1
tmin_cdecimal
5.6
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
0.9
peak_gust_msdecimal
4
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-06
tmax_cdecimal
22.2
tmin_cdecimal
7.2
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
0.6
peak_gust_msdecimal
3.6
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-07
tmax_cdecimal
16.1
tmin_cdecimal
7.2
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.8
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-08
tmax_cdecimal
17.2
tmin_cdecimal
8.3
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.5
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-09
tmax_cdecimal
25
tmin_cdecimal
6.7
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-10
tmax_cdecimal
23.3
tmin_cdecimal
7.8
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.4
peak_gust_msdecimal
4

first 10 of 109,214 rows · your queries use all of them

solution.sql
 
Output

Hints and the answer are always available. Using them counts the question as solved with help.

Last known daily maximum — SQL practice · Runtime