Since this thread was very useful for me, I decided to share my solution.
I had a similar problem, perhaps more generally applicable than this specific comparison with one set. I tried to find the identifier of an element that had a set of multi-element child elements that matched a set of queries from elements with multiple elements.
Relevant schema information:
table events, pk id table solutions, pk id, fk event_id -> events table solution_sources, fk solutionid -> solutions columns unitsourceid, alpha
Query: find the solution for the event with ID 110, which has a set of solution_sources resources that correspond to the set (unitsourceid, alpha) in ss_tmp. (This can also be done without the tmp table, I reckon.)
Decision:
with solutionids as ( select y.solutionid from ( select ss.solutionid, count(ss.solutionid) x from solutions s, solution_sources ss where s.event_id = 110 and ss.solutionid = s.id group by ss.solutionid ) y where yx = ( select count(*) from ss_tmp ) ) select solutionids.solutionid from solutionids where ( select case when count(*) = ( select count(*) from ss_tmp ) then true else false end from ( SELECT unitsourceid, alpha FROM solution_sources where solutionid = solutionids.solutionid INTERSECT SELECT unitsourceid, alpha FROM ss_tmp ) x )
Tested against a test request of 4 elements and a test db with the corresponding solution (the same number of children, each of which corresponds), several completely inappropriate solutions and 1 solution having 3 corresponding children, 1 solution in which there were all 4 corresponding children , plus an extra child, and 1 decision in which there were 4 children, of which 3 out of 4 matched the request. Only the true match identifier returned.
thanks a lot - linus
user1978162
source share