Here's the raw query I feed to the ORM I'm using:
SELECT
"fightEventId"
FROM
${EDBTableNames.LOCATION_TIME_SLOT} AS lts
WHERE
"fightId" IN (
SELECT
f.id
FROM
${EDBTableNames.FIGHTS} AS f
WHERE
f.status = '${EFightStatus.CONFIRMED}'
)
AND "fightEventId" IN (
SELECT
fe.id
FROM
${EDBTableNames.FIGHT_EVENTS} AS fe
WHERE
${status.includes(EFightEventStatus.ONGOING)}
AND (
NOW() at time zone 'utc' >= fe.from AND NOW() at time zone 'utc' <= fe.to
)
OR ${status.includes(EFightEventStatus.UPCOMING)} AND NOW() at time zone 'utc' <= fe.to
OR ${status.includes(EFightEventStatus.FINISHED)} AND NOW() at time zone 'utc' > fe.to
ORDER BY fe."from" ASC
)
GROUP BY "fightEventId"
HAVING
COUNT("fightId") > ${SHOW_WITH_NUMBER_OF_FIGHTS}
LIMIT ${limit}
OFFSET ${page * limit};
The problem with this query is that even though fight events are ordered by the "from" date: ORDER BY fe."from" ASC
, this subquery order is not maintained in the whole query. I need it to be maintained.
What would be the right way to do this? By the "right way" I mean performance and clarity.
Here is a bunch of options, but I'm a little bit confused as to which one to go for.
P.S.
SHOW_WITH_NUMBER_OF_FIGHTS
is an integer and as of now it's required to be equal to 4.