I have a database with three tables: userid_tbl, need_tbl, have_tbl
create table userid_tbl
(user_id varchar2(15) not null primary key);
create table need_tbl
(user_id varchar2(15) not null,
have_item varchar2(100) not null,
foreign key (user_id) references userid_tbl (user_id)
);
create table have_tbl
(user_id varchar2(15) not null,
have_item varchar2(100) not null,
foreign key (user_id) references userid_tbl (user_id)
);
Each user can create an unlimited number of records of needs or services they can provide to others in the database. The items are from a predetermined list.
After doing a join on the need and have table, I have matched up the needs and the wants, and discarded the requests that cannot be fulfilled by any user in a view that looks like this:
Item: Need: Have:
1 Bob George
2 George Herbie
3 Herbie Bob
4 Larry Wally
5 Wally Larry
6 John George
I am now trying to write a query where I can calculate the number of permutations of each user having their requested needs fulfilled. For instance, in the example above, Bob, George, and Herbie can together fulfill each other's needs, as can Larry and Wally, but John cannot, so the total count would be 2.
I first started out doing this with a LOOP, but there has to be a simpler, less resource intensive way to do this with a single query.