There’s a good chance I’ll have to add a database file. I’ll need to remove some personal information first. As for the ambiguity, I’ll give you the full story and the actual code used so there’s no confusion/abstraction.
This database is a pilot’s logbook that I’ve been putting together over time, mostly from scratch, with a few lines of code copied from various online sources whenever I get stuck. Most electronic logbooks are spreadsheets and lack a lot of the analytical functions and queries I have made (mostly because, as we all know, spreadsheets perform poorly when there’s a lot of data to work with). Taking your code and adapting it to my query, I have the following code:
SELECT
"Aircraft Type",
SUM(COALESCE("ME ICUS Day", 0) +
COALESCE("ME ICUS Night", 0) +
COALESCE("ME Dual Day", 0)+
COALESCE("ME Dual Night", 0)+
COALESCE("ME Command Day", 0)+
COALESCE("ME Command Night", 0)+
COALESCE("SE ICUS Day", 0)+
COALESCE("SE ICUS Night", 0)+
COALESCE("SE Dual Day", 0)+
COALESCE("SE Dual Night", 0)+
COALESCE("SE Command Day", 0)+
COALESCE("SE Command Night", 0)+
COALESCE("Co-Pilot Day", 0)+
COALESCE("Co-Pilot Night", 0)+
COALESCE("Glider", 0)
) "Total Experience",
SUM("Instrument Flight") "Instrument Flight",
SUM("Command PAX") "Command PAX",
SUM("Command Landings") "Command Landings",
MAX(CASE --Problem Area
WHEN --Problem Area
"Total Command Landings" = 3 --Problem Area
THEN --Problem Area
DATEDIFF(DAY, "Date", CURRENT_DATE) --Problem Area
END --Problem Area
) "Days Since 3rd Last Landing" --Problem Area
FROM
(
SELECT
"Aircraft Type",
"Date",
"ME ICUS Day",
"ME ICUS Night",
"ME Dual Day",
"ME Dual Night",
"ME Command Day",
"ME Command Night",
"SE ICUS Day",
"SE ICUS Night",
"SE Dual Day",
"SE Dual Night",
"SE Command Day",
"SE Command Night",
"Co-Pilot Day",
"Co-Pilot Night",
"Glider",
"Instrument Flight",
"Command PAX",
"Command Landings",
CASE --Problem Area
WHEN --Problem Area
"Command Landings" > 0 --Problem Area
THEN --Problem Area
SUM("Command Landings") --Problem Area
OVER( --Problem Area
PARTITION BY "Aircraft Type"--Problem Area
ORDER BY "Date" DESC --Problem Area
) --Problem Area
END --Problem Area
"Total Command Landings" --Problem Area
FROM
"Logbook"
)
GROUP BY "Aircraft Type"
ORDER BY "Aircraft Type"
I’ve marked the problematic areas with comments (hopefully the formatting here works). Below, I’ve attached a screenshot of the outputs given by the current two queries on the left, and the output of the query using the code above on the right:
The code above outputs only three rows in the last column. If I compare them to the old queries, the numbers are correct, but for some reason, the rest of the numbers from the top old query did not end up in the new query. Below is the actual code for the old query:
SELECT
"Aircraft Type",
DATEDIFF(DAY, "Date", CURRENT_DATE) AS "Days Since 3rd Last Landing"
FROM
(
SELECT
"Date",
"Command Landings",
"Aircraft Type",
"Landing Count",
ROW_NUMBER() OVER (PARTITION BY "Aircraft Type" ORDER BY "Date" DESC) AS "rn"
FROM
(
SELECT
"Date",
"Command Landings",
"Aircraft Type",
SUM("Command Landings") OVER (PARTITION BY "Aircraft Type" ORDER BY "Date" DESC) AS "Landing Count"
FROM "Logbook"
)
WHERE "Landing Count" >= 3
)
WHERE "rn" = 1
Pretty much exactly what was shown in the first post, just with some of the column names changed.
So as it stands, your code is definitely a step in the right direction. Just a bit stumped on why it only works for some of the rows and not others. If you still need me to upload the database file, let me know, and I’ll work on it in the next couple of days, time permitting.