The task is to create a visual seat map for a specific aircraft model (in this case, 'Boeing 777-300'). The result should be a list of rows, where for each row and service class ('Business', 'Economy', 'Comfort'), an array of available seats is shown.
For example, for the 1st row of business class, it might look like this:
Step 1: Selecting the Right Aircraft.
seats table with the aircrafts_data table on the common field aircraft_code.WHERE clause, specify the desired aircraft model.Step 2: Splitting the Seat Number into Row and Letter.
seats table has a format like 12A, 30K, where 12 is the row number, and A is the seat letter in the row.seat_no string. For example, using the rtrim function or regular expressions.seat_no string (e.g., with the right function).Step 3: Grouping the Seats.
fare_conditions).GROUP BY clause must include both the calculated row number and the service class.Step 4: Aggregating Seat Letters into an Array.
A, B, C, etc.) into a single list or array.json_agg (in PostgreSQL) is ideal for this, as it creates a JSON array from the values.json_agg function.Step 5: Final Sorting.
select
rtrim(seat_no, 'ABCDEFGHKJ')::int "row",
json_agg(right(seat_no, 1) order by right(seat_no, 1)) seats ,
fare_conditions
from seats
join aircrafts_data using(aircraft_code)
where aircrafts_data.model->>'en' = 'Boeing 777-300'
group by rtrim(seat_no, 'ABCDEFGHKJ'), fare_conditions
order by "row";