I have a table that stores a list of the people who 'subscribe' to certain events. this table has 2 columns: eventName and principalId. The latter is either a userId or a groupId. I'd like to issue a query through the API that takes my list and expands the groups to find all the distinct users subscribing to a given event. the following works, but for my own edification is there a more elegant method to do what i'm trying? not that this is that hard, but it seemed like it ought to be possible in a single query.
--first find groups
select
n.notificationtype,
n.recipient,
m.Userid,
from ehr.notificationrecipients n
left join core.members m on (n.recipient = m.GroupId)
where m.userid is not null
union all
--then union to users
select
n.notificationtype,
n.recipient,
u.UserId,
from ehr.notificationrecipients n
left join core.users u on (n.recipient = u.userid)
where u.userid is not null
thanks. |