Register  Login  
  February 5, 2012  
Forum    
Forum Minimize
 
ForumForumDiscussionsDiscussionsSQL Tips and Tr...SQL Tips and Tr...Stuck on getting a view to workStuck on getting a view to work
Disabled 
Previous
 
Next Next
New Post
 5/5/2010 10:54 PM
 

Sorry couldn't help it saw an empty forum thought it deserved a post :)

I am having a problem in getting some SQL to work. I have a set of User roles which are all members of a particular role group. Each user can be a member of multiple roles in the group though the expiry dates will all be different. I am trying to create a view that will contain the UserRoleID, UserID, RoleID, ExpiryDate, IsTrialUsed, EffectiveDate and RoleName of the latest expiry date for each user just for the particular role group.

I have come up with the following SQL which seems to only work when each user just has a single userrole in the group but not when there are multiple. I can't work out how to return just the 1 record for each user having the latest expiry date. Hope someone can help.

SELECT UserRoles.UserRoleID, UserRoles.UserID, UserRoles.RoleID, UserRoles.ExpiryDate, UserRoles.IsTrialUsed, UserRoles.EffectiveDate, Roles.RoleName,
Roles.RoleGroupID
FROM UserRoles INNER JOIN
Roles ON UserRoles.RoleID = Roles.RoleID INNER JOIN
(SELECT UserID, MAX(ExpiryDate) AS MaxExpDate
FROM UserRoles AS UserRoles_1
GROUP BY UserID) AS B ON UserRoles.UserID = B.UserID AND UserRoles.ExpiryDate = B.MaxExpDate
WHERE (Roles.RoleGroupID = 4)
ORDER BY UserRoles.UserID

TIA

Antony

New Post
 5/14/2010 1:35 PM
 

Antony,

One issue I noticed is that your are returning the RoleID. This should be unique to each user role record per user. So, this will force multiple records upon return ( same with RoleName).

I you want just one record per user, you will need to define how to combine the various fields you want to view - like the MAX(ExpiryDate).

Paul.

Disabled 
Previous
 
Next Next
ForumForumDiscussionsDiscussionsSQL Tips and Tr...SQL Tips and Tr...Stuck on getting a view to workStuck on getting a view to work

Print  

  Home | Blog | Forum | Subscriptions | Free Modules | Videos   Page generated in 0.0939036 seconds.
  Copyright 2010 by TressleWorks   Terms Of Use | Privacy Statement