Please excuse all the edits (I’m a SQL newb) and I could be wrong but I believe the inner query…
SUM(distance) AS flight_distance
GROUP BY 1,2,3
…is being set to a variable of “a”. In turn, the outer queries runs a query on the “a” variable or table.
The inner query is used to create an organized table sorted by month, day of the week, and then I believe for cosmetic reasons(?), month date. The ‘flight_distance’ is a sum of all miles flown across all flights for that day in the month.
The outer query queries this cleaned up and organized temporary table ‘a’ and groups the final output by departure month, then all of the Mondays, Tuesdays, Wednesday’s, etc. for that month and then the average flight distances for each of those sets of days of the week.
In the end, we have the average total distance flown by day of week in each month.
To understand what was happening, it was helpful to run the inner query in the example first and see the table results and then add the outer query around it and again, see the results.