I got myself stuck onto a very strange problem. Am I dumb, or there is no way to solve it ??? Cause it's diving me MAD.
I have simplified the problem:
Suppose I have these two tables:
tblPers
__________________
| IDPers | Name |
|--------|---------
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
| 5 | EEEE |
------------------
tblSales
__________________
| IDPers | IDProd |
|--------|---------|
| 1 | P1 |
| 1 | P2 |
| 1 | P3 |
| 1 | P4 |
|--------+---------|
| 2 | P1 |
| 2 | P3 |
| 2 | P4 |
|--------+---------|
| 3 | P1 |
| 3 | P2 |
| 3 | P3 |
| 3 | P5 |
|--------+---------|
| 4 | P1 |
| 4 | P2 |
| 4 | P4 |
| 4 | P6 |
------------------
The persons in tblPers are selling products. In tblSales are the products sold by each person. I guess it's quite clear.
MY PROBLEM IS: I just wanna get the persons who sold the products P1 AND P2 AND P3 (or ANY OTHER list of products).
The firs query I tried was:
SELECT DISTINCTROW tblPers.IDPers, tblPers.Name
FROM tblPers INNER JOIN tblSales ON tblPers.IDPers = tblSales.IDPers
WHERE tblSales.Product In ("P1","P2","P3");
The result was predictible, and NOT correct:
__________________
| IDPers | Name |
|--------|---------
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
------------------
because only the persons 1 and 3 match the criteria (2 and 4 didn't sold P1 AND P2 AND P3 - as you can see if you look back in tblSales)
Then, I tried a lot of other combinations with imbricated queries.... with no result
Example:
SELECT tblPers.IDPers FROM tblPers WHERE tblPers.IDPers in
(SELECT IDPers FROM tblSales WHERE tblSales.IDProd In ("P1","P2","P3"))
The result was the same....... all the persons were returned...
As I already said, the problem seem so simple, but I can't figure it out.
Using VBA I solved it, with a few LOOPS through some recordsets.
But I'm too damn stubborn, and I wanna solve it ONLY using SQL. And there's gotta be a way to solve it in ONLY ONE query.
Now I realized that, in order to solve the problem, I gotta define a criteria that should NOT check the existance of a SINGLE product in a list of products; my criteria should check if a WHOLE list of products (here - P1 and P2 and P3) is contained in another list of products, grouped on some field (here - the products grouped by each person, in tblSales ).
All my attempts using imbricated SQL did not satisfy this condition. I tried to use ALL, ANY, SOME, EXISTS.... but I reached nowhere.....
I know that somehow I gotta group the products by every person, and then I must compare my list of products with the grouped ones.
I still think there is something that I'm missing.... that I can't see the forest because of the trees.
But there is also a dark thought in my mind: that IT IS NOT POSSIBLE TO SOLVE THIS QUERY WITH ACCESS SQL !!!!
I usually solve by myself any of my problems, no matter how long it would take.
But I see no solution in this case.
Is there anyone who could give me an idea ?
Thanx in advance..
I have simplified the problem:
Suppose I have these two tables:
tblPers
__________________
| IDPers | Name |
|--------|---------
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
| 5 | EEEE |
------------------
tblSales
__________________
| IDPers | IDProd |
|--------|---------|
| 1 | P1 |
| 1 | P2 |
| 1 | P3 |
| 1 | P4 |
|--------+---------|
| 2 | P1 |
| 2 | P3 |
| 2 | P4 |
|--------+---------|
| 3 | P1 |
| 3 | P2 |
| 3 | P3 |
| 3 | P5 |
|--------+---------|
| 4 | P1 |
| 4 | P2 |
| 4 | P4 |
| 4 | P6 |
------------------
The persons in tblPers are selling products. In tblSales are the products sold by each person. I guess it's quite clear.
MY PROBLEM IS: I just wanna get the persons who sold the products P1 AND P2 AND P3 (or ANY OTHER list of products).
The firs query I tried was:
SELECT DISTINCTROW tblPers.IDPers, tblPers.Name
FROM tblPers INNER JOIN tblSales ON tblPers.IDPers = tblSales.IDPers
WHERE tblSales.Product In ("P1","P2","P3");
The result was predictible, and NOT correct:
__________________
| IDPers | Name |
|--------|---------
| 1 | AAAA |
| 2 | BBBB |
| 3 | CCCC |
| 4 | DDDD |
------------------
because only the persons 1 and 3 match the criteria (2 and 4 didn't sold P1 AND P2 AND P3 - as you can see if you look back in tblSales)
Then, I tried a lot of other combinations with imbricated queries.... with no result
Example:
SELECT tblPers.IDPers FROM tblPers WHERE tblPers.IDPers in
(SELECT IDPers FROM tblSales WHERE tblSales.IDProd In ("P1","P2","P3"))
The result was the same....... all the persons were returned...
As I already said, the problem seem so simple, but I can't figure it out.
Using VBA I solved it, with a few LOOPS through some recordsets.
But I'm too damn stubborn, and I wanna solve it ONLY using SQL. And there's gotta be a way to solve it in ONLY ONE query.
Now I realized that, in order to solve the problem, I gotta define a criteria that should NOT check the existance of a SINGLE product in a list of products; my criteria should check if a WHOLE list of products (here - P1 and P2 and P3) is contained in another list of products, grouped on some field (here - the products grouped by each person, in tblSales ).
All my attempts using imbricated SQL did not satisfy this condition. I tried to use ALL, ANY, SOME, EXISTS.... but I reached nowhere.....
I know that somehow I gotta group the products by every person, and then I must compare my list of products with the grouped ones.
I still think there is something that I'm missing.... that I can't see the forest because of the trees.
But there is also a dark thought in my mind: that IT IS NOT POSSIBLE TO SOLVE THIS QUERY WITH ACCESS SQL !!!!
I usually solve by myself any of my problems, no matter how long it would take.
But I see no solution in this case.
Is there anyone who could give me an idea ?
Thanx in advance..