Having a problem with a group by clause, and the case statement(using max and min). Basically I have a permissions table holding permissions, the problem I'm having is making permissions for individuals take precedence over group rights(a user with access should always have access if that user that rights specified in the usergroupright table). Here are the tables:UserGroupRight (RightAllow 1 is allow, -1 is deny)UserID RightID RightAllow------ ------- ----------1 1 11 2 11 3 -11 4 -12 12 22 32 4
UserGroup (UserType 1 is a group, 0 is a user)UserID Name UserType----- ----- --------1 John 02 Admin 1
UserGroupLine (GroupID relates to UserID in UserGroup)UserID GroupID------ -------1 2
AreaRightRightID RightName------- ---------1 Access2 Create3 Modify4 Delete
Here's the query without the precedence logic(this is just with a permissions returning -1 if the least is -1)select ar.RightID, ar.RightName, RightAllow = (case when min(ugr.RightAllow) =-1 then -1 else max(ugr.RightAllow) end)into #tmprightfrom usergroupright ugr, arearight arwhere ugr.RightID = ar.RightID and (ugr.UserID in (select groupID from usergroupline where UserID = @UserID) or (ugr.Userid = @UserID))group by ar.RightID, ar.RightNameany help?