I designed a ticket booking system for flights. After I made some inserts, I want to write a query to show top 3 visited countries.
This is what I wrote:
select * from ( select flightnumber, count(*) as NumberOfFlights from ticket group by flightnumber order by NumberOfFlights desc) where rownum < 4;
My question is how to select Country column from Airport table such that to the current query result columns "FlightNumber" and "NumberOfFlights" to add "Country" name which corresponds to FlightNumber.
One example: Airport table
insert into airport(icao_code, name, city, country) values ('PJAN', 'IS Airport', 'Iasi', 'Romania');
insert into airport(icao_code, name, city, country) values ('SBJN', 'SB Airport', 'Sibiu', 'Romania');
insert into airport(icao_code, name, city, country) values ('TMAN', 'TM Airport', 'Timisoara', 'Romania');
insert into airport(icao_code, name, city, country) values ('CJAN', 'CJ Airport', 'Cluj', 'Romania');
insert into airport(icao_code, name, city, country) values ('TKAN', 'TK Airport', 'Cluj', 'Turkey');
insert into airport(icao_code, name, city, country) values ('UKAN', 'UK Airport', 'London', 'UK');
insert into airport(icao_code, name, city, country) values ('ITAN', 'IT Airport', 'Roma', 'Italy');
insert into airport(icao_code, name, city, country) values ('SPAN', 'SP Airport', 'Madrid', 'Spain');
insert into airport(icao_code, name, city, country) values ('FRAN', 'FR Airport', 'Paris', 'France');
insert into airport(icao_code, name, city, country) values ('DEAN', 'DE Airport', 'Berlin', 'Germany');
insert into airport(icao_code, name, city, country) values ('GRAN', 'GR Airport', 'Athens', 'Greek');
Flight Table
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(1,to_date('2022/11/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/11/01 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'PJAN', 'GRAN',500,231);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(2,to_date('2022/02/15 08:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/02/16 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'SBJN', 'GRAN',500,232);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(3,to_date('2022/10/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/10/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'GRAN', 'CJAN',400,232);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(4,to_date('2022/09/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/09/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'GRAN', 'TMAN',700,152);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(5,to_date('2022/08/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/08/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'PJAN', 'GRAN',600,231);
--Paris-Berlin/Berlin-Paris
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(6,to_date('2022/05/06 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/06 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 800,'FRAN', 'DEAN',300,152);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(7,to_date('2022/05/07 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/07 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 800,'DEAN', 'FRAN',300,152);
-- Spain-Turkey/Turkey-Spain
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(8,to_date('2022/05/07 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/09 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'SPAN', 'TKAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(9,to_date('2022/05/08 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/10 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'SPAN', 'TKAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(10,to_date('2022/05/17 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/19 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'TKAN', 'SPAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(11,to_date('2022/05/18 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/20 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'TKAN', 'SPAN',800,112);
Ticket table
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(1,200,'1232sde2asmd',7,37);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(2,210,'1232sde2asnd',7,36);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(3,250,'1232sde2astd',7,35);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(4,270,'1232sde2as2d',7,34);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(5,280,'1232sde2as0d',4,33);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(6,300,'1232sde2as9d',4,32);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(7,100,'1232sdv2as3d',4,31);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(8,200,'1232vde2as4d',1,30);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(9,120,'1252sde2as3d',2,29);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(10,290,'1232sde2ax3d',3,28);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(11,50,'1232sdy2as3d',3,27);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(12,90,'1232sae2as3d',6,26);
In the Ticket table I inserted 4 flights to France, 3 flights to Greece and another 2 to Greece (my mistake). It should be another Country.
So my query gives me:
FlightNumber NumberOfFlights
7 4
4 3
3 2
I want a result like that:
FlightNumber Country NumberOfFlights
7 France 4
4 Greece 3
3 Greece(mistake,some random country) 2
CodePudding user response:
You need to join the tables together, then group by the country.
On Oracle 12c you can even use the newer offset
syntax, and this will work in SQL Server also.
select
a.country,
count(*) as NumberOfTickets
from ticket as t
join flight as f on f.flightnumber = t.flightnumber
join airport as a on a.icao_code = f.airportid_arrival
group by
a.country
order by
NumberOfFlights desc
offset 0 rows fetch next 3 rows only;
If you wanted to get the top three flights, you could add flightnumber
to the group by
select
t.flightnumber,
a.country,
count(*) as NumberOfTickets
from ticket as t
join flight as f on f.flightnumber = t.flightnumber
join airport as a on a.icao_code = f.airportid_arrival
group by
t.flightnumber,
a.country
order by
NumberOfFlights desc
offset 0 rows fetch next 3 rows only;
CodePudding user response:
Maybe using oracle rank function. Something like this for oracle 11g:
select *
from (select country, flightCnt, dense_rank () over( order by flightCnt) as rnk
from (select a.country, count(f.flightNumber) as flightCnt
from ticket t, flight f, airport a
where t.flightNumber = f.flightNumber
and f.airportId_arrival = a.icao_code
group by a.country)
)
where rnk < 4
;