Created
May 22, 2025 11:48
-
-
Save Alxandr/c2f6732ce49612b1437f4270c6114d7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with uuids_by_party_id as ( | |
select p."uuid" | |
from register.party p | |
where p.id = any (:partyids) | |
), uuids_by_user_id as ( | |
select u."uuid" | |
from register."user" u | |
where u.user_id = any (:userids) | |
), uuids as ( | |
select "uuid" | |
from uuids_by_party_id | |
union | |
select "uuid" | |
from uuids_by_user_id | |
) | |
select | |
p.uuid p_uuid, | |
p.id p_id, | |
p.party_type p_party_type, | |
p.display_name p_display_name, | |
p.person_identifier p_person_identifier, | |
p.organization_identifier p_organization_identifier, | |
u.user_id, | |
u.is_active | |
from register.party p | |
left join register."user" u using (uuid) | |
where p."uuid" in (select "uuid" from uuids) | |
order by p.uuid, u.is_active DESC, u.user_id desc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
p.uuid p_uuid, | |
p.id p_id, | |
p.party_type p_party_type, | |
p.display_name p_display_name, | |
p.person_identifier p_person_identifier, | |
p.organization_identifier p_organization_identifier, | |
ua.user_ids u_user_ids | |
FROM register.party p | |
LEFT JOIN ( | |
SELECT | |
u.uuid, | |
array_agg(u.user_id ORDER BY u.is_active DESC, u.user_id DESC) AS user_ids | |
FROM register.user u | |
WHERE u.is_active OR u.user_id = ANY (:userids) | |
GROUP BY u.uuid | |
) ua USING (uuid) | |
WHERE ( | |
p.id = ANY(:partyids) | |
OR :userids && ua.user_ids | |
) | |
ORDER BY p.uuid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment