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(owner_member.squad_id AS TEXT) AS ownerSquadId, owner_squad.name AS ownerSquadName, CAST(flag.overtaker_user_profile_id AS TEXT) AS overtakerProfileId, strftime('%Y-%m-%dT%H:%M:%SZ', flag.overtake_end_time, 'unixepoch') AS overtakeEndTime, CASE WHEN element.owner_profile_id IS NULL THEN 'unknown' ELSE 'direct' END AS ownershipConfidence, 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 LEFT JOIN squad_member owner_member ON owner_member.user_profile_id = element.owner_profile_id LEFT JOIN squad owner_squad ON owner_squad.id = owner_member.squad_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)