Oracle consulta auto-unión?

I've simplified this example but hopefully the example provides enough substance to make sense.

If I have a table such as the following...

ITEM GROUP
---- ----- 
 A     1
 B     1
 C     1
 D     2
 E     2
 F     3
 G     4

... and I am provided items A, B, D and F, I would like to contruct a query that will return those details along with the additional items in the associated groups, C and E.

It seems that I should be able to do some sort of inner join but I'm not clear on how it could be done. It would be best if this were done in a single query due to the constraints of the environment.

¡Muchas gracias!

preguntado el 03 de mayo de 12 a las 16:05

what result you are expecting? -

1 Respuestas

If I understand you correctly, this would work.

SELECT item,
       group_num
  FROM table_name
 WHERE grroup_num IN (SELECT group_num
                        FROM table_name
                       WHERE item IN ('A', 'B', 'D', 'F'))

You could also write it as an EXISTS

SELECT item,
       group_num
  FROM table_name a
 WHERE EXISTS( SELECT 1
                 FROM table_name b
                WHERE a.group_num = b.group_num
                  AND b.item IN ('A','B','D','F') )

contestado el 03 de mayo de 12 a las 16:05

Perfect. This was what I was looking for. My actual implementation is much more complex but distilling the problem down to a simple question and solution really made the difference. Thanks much for your help! - McArthey

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.