Thanks to the help on my last question, I now have one final step to fully grasp this method of querying for my problem.
Given these two tables:
airlines_detail:
| airplane_id | airline_id | total_seats |
|---|---|---|
| 2187 | 425 | 260 |
| 4361 | 747 | 290 |
| 3478 | 747 | 270 |
| 7292 | 425 | 250 |
| 5833 | 425 | 310 |
| 3472 | 425 | 300 |
| 4472 | 747 | 290 |
| 2624 | 425 | 320 |
Bookings:
| airplane_id | booked |
|---|---|
| 2187 | 40 |
| 4361 | 30 |
| 4361 | 10 |
| 5833 | 30 |
| 4361 | 30 |
| 2624 | 30 |
| 4472 | 40 |
| 4472 | 40 |
| 2624 | 10 |
| 7292 | 20 |
| 2187 | 20 |
| 4472 | 30 |
| 3478 | 20 |
| 3472 | 40 |
| 4472 | 30 |
| 4472 | 10 |
| 4361 | 20 |
| 3478 | 30 |
| 2187 | 30 |
| 2187 | 10 |
I need to get this result:
| airline_id | airplanes |
|---|---|
| 425 | 7292 |
| 747 | 4361 |
The result is the airplane_id whose SUM of empty seats is closest to the AVG number of empty seats per each airline_id (in this case, there's only 425 and 747 to worry about).
So, we would need to find the AVG of SUM(empty_seats) GROUP BY airline_id, then compare the SUM(empty_seats) GROUP BY airplane_id for each airline, and return a table that shows the airplanes that are closest to their respective airline's average of empty seats.
For airline with id 425 the average number of empty seats are 242. Therefore the airplane which is closest to this average is 7292.
Equipped with the answer from the previous question in getting the sum of empty seats for each plane, I'm hoping this next step is a simple one for a guru.