INTERSECT set operation or it's equivalent

Posts   
 
    
Posts: 30
Joined: 08-Apr-2008
# Posted on: 30-Jun-2008 06:34:38   

Hi!

I'm trying to write a query involving the INTERSECT operator that goes like this:

SELECT UserId, UserName FROM GroupMemberShip JOIN User ON GroupMembership.UserId = User.UserId WHERE GroupId = 1 INTERSECT SELECT UserId, UserName FROM GroupMemberShip JOIN User ON GroupMembership.UserId = User.UserId WHERE GroupId = 4 ...

What I'm trying to retrieve is the list of users who belong to both Group 1, Group 4 and possibly more (there's a variable number of groups, not just 2). I haven't tried set operations with LLBL Gen Pro before.

If set operations aren't supported, perhaps I could re-write the query to something like: SELECT UserId FROM GroupMembership WHERE GroupId IN (1, 4, ...) GROUP BY UserId HAVING COUNT(1) = n; Replace 'n' in the "COUNT(1) = n" expression with the number of groups you are searching for.

I haven't really spent much time with group functions in LLBLGen so, for me, this seems a little complex to do (although this SQL would be run more efficiently due to the lack of set operations).

Any help appreciated.

Regards, Nitin

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 30-Jun-2008 08:10:58   

If set operations aren't supported, perhaps I could re-write the query to something like: SELECT UserId FROM GroupMembership WHERE GroupId IN (1, 4, ...) GROUP BY UserId HAVING COUNT(1) = n; Replace 'n' in the "COUNT(1) = n" expression with the number of groups you are searching for.

I think you'll have to use this approach

A similar question was posted here: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=11867

Posts: 30
Joined: 08-Apr-2008
# Posted on: 30-Jun-2008 08:36:24   

Walaa wrote:

If set operations aren't supported, perhaps I could re-write the query to something like: SELECT UserId FROM GroupMembership WHERE GroupId IN (1, 4, ...) GROUP BY UserId HAVING COUNT(1) = n; Replace 'n' in the "COUNT(1) = n" expression with the number of groups you are searching for.

I think you'll have to use this approach

A similar question was posted here: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=11867

Thanks, I'll try to use the FieldCompareSetPredicate for the IN operator and will check the documentation for the GROUP BY, HAVING and COUNT operators. I'll keep you posted on my mileage.