If I had a table PERMISSIONSthat looked like this:
PERSON PERMISSION
------ ----------
Bob red
John red
John blue
Mary red
Mary blue
Mary yellow
and a THINGS table that looks like this:
THING PERMISSION
----- ----------
apple red
eggplant red
eggplant blue
I'm trying to come up with a clean SQL query that will let me know that I PERSONhave access to those THINGs. Basically, I want a query that will look something like this:
SELECT person
FROM ... vague handwaving here ...
WHERE thing = 'eggplant'
and return it to John and Mary. The key point is the number of permissions required to access the item, arbitrarily.
I feel this should be obvious, but I just can't come up with an elegant solution. It is recommended that you use Oracle compatible solutions.
Edit:
Kosta JBrooks . Kosta, , 3x Kosta 4x JBrooks ( JBrooks , , , ).
SELECT p.person, num_permission, COUNT(p.person)
FROM permissions p
INNER JOIN (
SELECT permission,
COUNT(1) OVER (PARTITION BY thing) AS num_permission
FROM things
WHERE thing = 'eggplant'
) t ON t.permission = p.permission
GROUP BY p.person, num_permission
HAVING COUNT(p.person) = num_permission