Hello community, I understand basic SQL, but subqueries are rather confusing…
I’m unsure what the GROUP BY 1,2,3
does? And why are there two GROUP BY
's in the outer query?
SELECT a.dep_month,
a.dep_day_of_week,
AVG(a.flight_count) AS average_flights
FROM (
SELECT dep_month,
dep_day_of_week,
dep_date,
COUNT(*) AS flight_count
FROM flights
GROUP BY 1,2,3
) a
GROUP BY 1,2
ORDER BY 1,2;