SELECT CAST(flag.element_id AS TEXT) AS flagId, CAST(flag.element_id AS TEXT) AS entityId, CAST(element.base_id AS TEXT) AS baseId, CAST(element.owner_profile_id AS TEXT) AS ownerProfileId, CAST(owner.prisoner_id AS TEXT) AS ownerPlayerId, CAST(flag.overtaker_user_profile_id AS TEXT) AS overtakerProfileId, datetime(flag.overtake_end_time, 'unixepoch') AS overtakeEndTime, element.location_x AS x, element.location_y AS y, element.location_z AS z FROM base_element_flag flag JOIN base_element element ON element.element_id = flag.element_id LEFT JOIN user_profile owner ON owner.id = element.owner_profile_id WHERE (:flagId IS NULL OR CAST(flag.element_id AS TEXT) = :flagId) AND (:ownerProfileId IS NULL OR CAST(element.owner_profile_id AS TEXT) = :ownerProfileId) ORDER BY flag.element_id LIMIT COALESCE(:limit, 500)