You are overcomplicating things with the use of the aggregate function MAX()
.
This part of your code:
EXISTS (
SELECT 1
FROM (
SELECT MAX(TourCompletionDateTime) AS TourCompletionDateTime
FROM Details
WHERE TourCompletionDateTime < '2022-07-26T09:36:00.730589Z'
)
WHERE TourCompletionDateTime IS NOT NULL
)
is equivalent to just:
EXISTS (
SELECT 1
FROM Details
WHERE TourCompletionDateTime < '2022-07-26T09:36:00.730589Z'
)
See the demo.
0
solved Generalized way to remove empty row given by aggregate functions in SQLite