Query: Reworked fetching of tickets for cached queries to only use a single SQL to generate the combined ticket list. This should handle changes better, including fix for the sorting issue in #7039.
Also used the opportunity to remove some unused imports.