SELECT CAST(profile.id AS TEXT) AS userProfileId, account.id AS steamId, CAST(prisoner.id AS TEXT) AS gamePlayerId, COALESCE(NULLIF(profile.name, ''), NULLIF(account.name, ''), account.id) AS displayName, account.last_direct_connection_address AS lastLoginIp, account.creation_time AS registeredAt, CAST(member.squad_id AS TEXT) AS squadId, squad.name AS squadName, profile.fame_points AS famePoints, profile.money_balance AS moneyBalance, MAX(CASE WHEN currency.currency_type = 1 THEN currency.account_balance END) AS normalBalance, MAX(CASE WHEN currency.currency_type = 2 THEN currency.account_balance END) AS goldBalance, entity.location_x AS x, entity.location_y AS y, entity.location_z AS z, profile.last_login_time AS lastLoginTime, profile.last_logout_time AS lastLogoutTime, strftime('%Y-%m-%dT%H:%M:%SZ', prisoner.last_save_time, 'unixepoch') AS lastSaveTime FROM user account LEFT JOIN user_profile profile ON profile.user_id = account.id LEFT JOIN prisoner ON prisoner.id = profile.prisoner_id LEFT JOIN prisoner_entity ON prisoner_entity.prisoner_id = prisoner.id LEFT JOIN entity ON entity.id = prisoner_entity.entity_id LEFT JOIN squad_member member ON member.user_profile_id = profile.id LEFT JOIN squad ON squad.id = member.squad_id LEFT JOIN bank_account_registry bank ON bank.account_owner_user_profile_id = profile.id LEFT JOIN bank_account_registry_currencies currency ON currency.bank_account_id = bank.id WHERE (:userProfileId IS NULL OR CAST(profile.id AS TEXT) = :userProfileId) AND (:steamId IS NULL OR account.id = :steamId) AND (:search IS NULL OR COALESCE(NULLIF(profile.name, ''), NULLIF(account.name, ''), account.id) LIKE '%' || :search || '%') AND (:activeWithinSeconds IS NULL OR profile.last_logout_time IS NULL OR profile.last_login_time > profile.last_logout_time OR profile.last_login_time >= strftime('%Y-%m-%dT%H:%M:%fZ', 'now', printf('-%d seconds', :activeWithinSeconds)) OR prisoner.last_save_time >= CAST(strftime('%s', 'now') AS INTEGER) - :activeWithinSeconds) GROUP BY account.id ORDER BY profile.last_login_time DESC LIMIT COALESCE(:limit, 500)