EDITED: I am trying to CASE a query from my table when a result meets 2 sets of criteria (Flight Time & Tail Number), but I am receiving the wrong result. I only started SQL a few weeks ago for an upcoming school assignment, but am having trouble with this query.
SELECT FlightNumber AS 'Flight Number', Date, Aircraft, Aircraft_Manufacturer AS 'Aircraft Manufacturer', Tail_Number AS 'Tail Number', Departure, Arrival, FlightTime AS 'Flight Time', Instructor, Passengers,
CASE
WHEN SUM(FlightTime) >= 20 AND Tail_Number LIKE '24-%' THEN "RAAus Recreational Pilots Certificate (RPC)"
WHEN SUM(FlightTime) >= 25 AND Tail_Number LIKE '24-%' THEN "RAAus RPC Passenger Endorsment"
WHEN SUM(FlightTime) >= 32 AND Tail_Number LIKE '24-%' THEN "RAAus RPC Cross Country Endorsment"
WHEN SUM(FlightTime) >= 7 AND Tail_Number LIKE 'VH-%' THEN "RPC Conversion to CASA Recreational Pilot License (RPL)"
ELSE 'Not Eligible For License'
END AS "License Eligibility"
FROM Flight_Log
GROUP BY FlightNumber
ORDER BY FlightNumber
Expected Results:
Flight Number | Flight Time | Licence Eligibility |
---|---|---|
1 | 7 | Not Eligible for License |
2 | 6 | Not Eligible for License |
3 | 8 | Not Eligible for License |
4 | 5 | RAAus Recreational Pilots Certificate (RPC) |
Once the cumulative hours reaches 20 or more to show the WHEN statements.
My Table Creation with the INSERT INTO looks like this for anyone wondering (I have removed a few rows from the end):
CREATE TABLE IF NOT EXISTS Flight_Log (
`FlightNumber` INTEGER PRIMARY KEY AUTO_INCREMENT,
`Date` DATE,
`Aircraft` VARCHAR(50),
`Aircraft_Manufacturer` VARCHAR(50),
`Tail_Number` VARCHAR(50),
`MTOW` INTEGER,
`Manufacture_Year` YEAR,
`Aircraft_Type` VARCHAR(50),
`Departure` CHAR(4),
`Arrival` CHAR(4),
`FlightTime` INTEGER,
`Instructor` BOOLEAN,
`Passengers` INTEGER
);
INSERT INTO Flight_Log VALUES (1,'2022-10-08','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',1,'1',0);
INSERT INTO Flight_Log VALUES (2,'2022-12-03','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',3,'1',0);
INSERT INTO Flight_Log VALUES (3,'2022-12-05','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',3,'1',0);
INSERT INTO Flight_Log VALUES (4,'2022-12-08','Tecnam Sierra','Tecnam','24-7155',600,2002,'Recreational','YCDR','YCDR',3,'1',0);
INSERT INTO Flight_Log VALUES (5,'2022-12-17','Tecnam Sierra','Tecnam','24-7155',600,2002,'Recreational','YCDR','YCDR',3,'1',0);
INSERT INTO Flight_Log VALUES (6,'2022-12-27','Fly Synthesis Texan','Fly Synthesis','24-5285',600,1998,'Recreational','YCDR','YCDR',1,'1',0);
INSERT INTO Flight_Log VALUES (7,'2022-12-31','Tecnam Sierra','Tecnam','24-7155',600,2002,'Recreational','YCDR','YCDR',2,'0',0);
INSERT INTO Flight_Log VALUES (8,'2023-01-01','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',2,'0',0);
INSERT INTO Flight_Log VALUES (9,'2023-01-12','Tecnam Sierra','Tecnam','24-7155',600,2002,'Recreational','YCDR','YCDR',1,'0',0);
INSERT INTO Flight_Log VALUES (10,'2023-01-18','Tecnam Sierra','Tecnam','24-7155',600,2002,'Recreational','YCDR','YCDR',4,'0',0);
INSERT INTO Flight_Log VALUES (11,'2023-01-27','Tecnam Sierra','Tecnam','24-7155',600,2002,'Recreational','YCDR','YCDR',3,'0',0);
INSERT INTO Flight_Log VALUES (12,'2023-02-03','Tecnam Sierra','Tecnam','24-7155',600,2002,'Recreational','YCDR','YCDR',3,'0',0);
INSERT INTO Flight_Log VALUES (13,'2023-02-14','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',1,'0',1);
INSERT INTO Flight_Log VALUES (14,'2023-02-14','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',1,'0',1);
INSERT INTO Flight_Log VALUES (15,'2023-03-14','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',1,'0',1);
INSERT INTO Flight_Log VALUES (16,'2023-03-14','Tecnam P-92 Eaglet','Tecnam','24-5955',600,1992,'Recreational','YCDR','YCDR',1,'0',1);
INSERT INTO Flight_Log VALUES (17,'2023-03-28','Cessna 172R','Cessna','VH-CFG',1111,1997,'Recreational','YCDR','YCDR',3,'1',0);
INSERT INTO Flight_Log VALUES (18,'2023-04-06','Cessna 172 G1000','Cessna','VH-IVW',1156,2013,'Recreational','YRED','YRED',3,'1',0);
INSERT INTO Flight_Log VALUES (19,'2023-04-19','Cessna 172 G1000','Cessna','VH-IVW',1156,2013,'Recreational','YRED','YCDR',2,'1',0);
INSERT INTO Flight_Log VALUES (20,'2023-04-23','Cirrus SR22','Cirrus','VH-EDH',1633,2014,'Recreational','YBAF','YBAF',2,'1',0);
INSERT INTO Flight_Log VALUES (21,'2023-05-02','Cirrus SR22','Cirrus','VH-EDH',1633,2014,'Recreational','YBAF','YBAF',2,'1',0);
What am I doing wrong? (I am using the newest version of MySQL (8.0.31))
2
Answers
When you are mixing aggregate functions (
SUM
in your case) and plain columns in select you will need aGROUP BY
statement telling over which set the aggregate function is calculated from.You are also using
FlightTim
andsum(FlightTime)
at same level. Over which set do you want thesum(FlightTime)
to be calculated?Modify the question to include sample data with expected results.
So I recreated your table:
Which gives me this:
Query you should have:
this gives me:
Just add the other columns you have with its alias’ and you should be good. If this isn’t what you want, please provide the exercise statement.
For your DATASET this query will be:
IMPORTANT: It is normal that the License Eligibility is an Error everywhere…
When running this query:
we can see that all flightnumbers with tail number starting with 24 are under 20 hours –> So error
And also the tailnumbers with VH are all under 7 –> Error.
If you add more data and more hours to all tail_numbers you will see my query works