Solution:
select A.GRUPO
from TABELA A join TABELA B on B.OBJETO = A.OBJETO
where B.GRUPO = 1
group by A.GRUPO
having count(A.OBJETO) = (select count(OBJETO) from TABELA where GRUPO = 1)
and count(A.OBJETO) = (select count(OBJETO) from TABELA where GRUPO = A.GRUPO)
This query works and seems to me quite simple.
Explaining the line of reasoning:
With the first part of the query (from select up to the Where) i return the records that contain at least one of the group 1 objects.
For better visualization, see this first part of the slightly modified query:
select A.ID, A.GRUPO, A.OBJETO
from TABELA A join TABELA B on B.OBJETO = A.OBJETO
where B.GRUPO = 1
The result of this query would be:
-------------------
ID | GRUPO | OBJETO
---|-------|-------
1 | 1 | 1
2 | 1 | 2
| |
3 | 2 | 1
4 | 2 | 2
| |
5 | 3 | 1
6 | 3 | 2
-7 | 3 | 3
| |
8 | 4 | 1
-9 | 4 | 3
| |
10 | 5 | 1
The marked records (-) were left out of the result, since its object does not correspond to any of the group 1 objects.
So see that so far I return only the groups that have some object of group 1, and also their respective object.
Let’s now visualize the grouping of this:
select A.GRUPO, count(A.OBJETO)
from TABELA A join TABELA B on B.OBJETO = A.OBJETO
where B.GRUPO = 1
group by A.GRUPO
Upshot:
---------------------
GRUPO | COUNT(OBJETO)
------|--------------
1 | 2
2 | 2
3 | 2
4 | 1
5 | 1
Now I will filter to keep only the groups whose totalization of objects is equal to the total of objects in group 1, adding the clause having:
select A.GRUPO, count(A.OBJETO)
from TABELA A join TABELA B on B.OBJETO = A.OBJETO
where B.GRUPO = 1
group by A.GRUPO
-- novo:
having count(A.OBJETO) = (select count(OBJETO) from TABELA where GRUPO = 1)
Upshot
---------------------
GRUPO | COUNT(OBJETO)
------|--------------
1 | 2
2 | 2
3 | 2
Group 3 has to leave because although the Count of it has stayed at 2 (because it actually has two objects matching the group 1 objects), in fact it has 3 objects at the base (one of them does not coincide with any group 1 object and so was outside the Count).
Then I add one more condition in the having, determining that only groups whose total number of objects in the base is equal to the total of objects in group 1:
select A.GRUPO, count(A.OBJETO)
from TABELA A join TABELA B on B.OBJETO = A.OBJETO
where B.GRUPO = 1
group by A.GRUPO
having count(A.OBJETO) = (select count(OBJETO) from TABELA where GRUPO = 1)
-- novo:
and count(A.OBJETO) = (select count(OBJETO) from TABELA where GRUPO = A.GRUPO)
Finally, I remove the Count of select because I do not wish it in the visualization of the result, and I arrive at the query that was presented at the beginning of the answer.
See on SQL Fiddle.
It worked wonderfully well. I could give a slight comment on the low side, just to not be completely lost?
– Papa Charlie
Of course, I did. I edited the answer to include an explanation. If you need any more details warns.
– bruno
Just about the use of
HAVING, COUNT e CASE
... Makes a combination with the total of object with the objects distinct from group 1, that’s it?– Papa Charlie
Yes, using having is to ensure that all objects in the group are in group 1. I edited the answer, let us know if you need any extra details.
– bruno
Show, I understood the application better now. I had already turned the SOEN but I found nothing that could help. Your answer was straight to the point.
– Papa Charlie
I’m just waiting for the time to offer reward to mark your response. I opened a META explaining.
– Papa Charlie