There are many users, many groups that users can be part of. Each group has a resource quota.
The tables are as follows:
User: UserId, LastName, FirstName, ...
Group: GroupId, GroupName, ...
UserGroup: GroupId, UserId (Users are in Groups)
Resource: ResourceId, ResourceName, ...
GroupResource: GroupId, ResourceId, Quota (Users in Groups can access certain Resources specified Quota times)
UserResourceAccess: UserId, ResourceId, InsertDate, ...
Of course, there are daily and monthly quotas, but that doesn’t matter for the purpose of this question.
So, when user #1 (being in groups #1, #2 with resource quotas 2 and 1 respectively) accesses resource #1 his total quota for that resource should be reduced by 1: (1+2) – 1 = 2 (times left to access).
But then, there’s another user #2 who’s a member of group #2 and he tries to access the resource #1…
Here’s the interesting part, user #1 already accessed that resource and his total quota was reduced 1 time (which now equals 2) but we don’t know which group quota was reduced (or do we?)
So the question is, how to implement this logic in SQL Server 2005/2008 (maybe even Denali:)?
How can we know that user #2 can actually access that resource one remaining time and force user #1’s action reduce his group #1’s quota by 1 and not group #2′?
If it still makes any sense to you, please solve this problem. 🙂 I’ve been thinkin’ about it for quite some time now.
Just to make it clear, I want to have a single select statement that would tell me what user #2’s quota is right now, right after user #1 accessed that resource. (hint: it should still be 1 and not 0)
Update
User #1 has an aggregate quota of all the groups that he’s in, hence his quota for resource #1 is 2+1 = 3; and the user #2’s quota for resource #1 is 1 because he’s only a member of group #2 (which has a quota of 1). When someone from any of your groups (that have resource quota for resource being accessed) accesses a resource, your aggregate quota for that resource is reduced accordingly (by as many times as that resource was accessed).
Update 2
The quota reduction rule is as follows: a group with the most quota left should be used for each access. So, in the first example, when user #1 accesses the resource we choose group #1 because it’s got a quota of 2 (which is > than 1). And the next time, when user #2 accesses the resource we choose any group because they both have a quota of 1 access left. That’s the business rule that was missing.
I believe I understand what you are asking, but I believe you have a flaw in your logic. You are asking how to reduce available usage for a group, but in reality, you are calculating available resource availability from the user’s groups, and decrementing it for the user, not the group.
If this is the case, you would aggregate your group resource availability, and subtract the user’s resource access counts.
UPDATE
Based on your last comment, I believe we’re getting on the same page. A group’s quota is fixed for all users in the group – if user #3 and user #4 are only in group #3, which has a quota of 2, user #3 could access twice, user #4 could access twice, or they could each access once.
Given this, you are trying to calculate which group’s quota is used when a user in multiple groups accesses a resource.
You are trying to determine the technical solution to an unwritten business rule – which group’s quota does a user pull from? You need to define that outside of the system first, and then implementing it is trivial. Which group should a user pull from? Is it based on number of users inside of the group? Does the group have a priority? Does the user have a preferred group? etc, etc.
Until you know the business rule, any solution you put in place is untestable – you don’t have anything to compare it to.