Re: SQL QUESTION (ADVANCED ??)
Date: 2 Sep 1994 12:17:00 +0100
Message-ID: <3471jc$prm_at_crocus.csv.warwick.ac.uk>
jos_at_zeus.wnc.nedlloyd.nl (Jos Kerssens) writes:
> I have some problems writing a statement that selects the
> required information in the following situation:
> I have two tables that have an n:m relationship, with an
> intersection table in between, as follows:
> ------- ------- -------
> | A |-------------<| B |>-------------| C |
> ------- ------- -------
> The relationships in table B could be like
> A1-C1
> A2-C2
> A2-C3
> A4-C4
> A5-C4
> A5-C5
> A6-C5
> A6-C6
> A4-C6
> Now, if I have a value for A, I want to select all other A's
> that are somehow connected to the given value for A.
> In case of A4, that would be A4, A5, A6.
Thinking in terms of groups (Group By, I presume you mean) isn't going to help you with this one...
Thinking in terms of Connect By isn't going to help you with this one, either...
You don't need PL/SQL for this one; try this
Select Distinct column_A From table_B X Where Exists (Select Null From table_B Y Where X.column_A = column_A And Exists (Select Null From table_B Where Y.column_C = column_C And column_A = '&value_A'))
Order By column_A;
Or even
Select a_whole_load_of_A_attributes From table_A X Where Exists (Select Null From table_B Y Where X.column_A = column_A And Exists (Select Null From table_B Z Where Y.column_A = column_A And Exists (Select Null From table_B Where Z.column_C = column_C And column_A = '&value_A')))
Order By column_A;
> Seems to me this is not such an extraordinary case so a solution
> must be around somewhere and might actually be simple.
Yep; you're right; quite common. And simple :-)
I use similar, but inverted, selects in some of my views for the following schema: we have a load of Members, some of whom can possess different memberShips (according to what they're doing; eg. staffship & studentship). Each memberShip can have any number of (within reason) Unix usercodes. Some of my views need to provide 'access privileges' to any Unix usercode as long as the Member, owning the Unix usercode, owns a 'privileged' Unix usercode.
> Thanks in advance,
You're welcome; hope this helps.
> Jos.
> --
> ------------------------------------------------------------------------
> Nedlloyd Computer Services _/ _/ _/_/_/_/ _/_/_/_/
> P.O. Box 2454 _/_/ _/ _/ _/
> 3000 CL Rotterdam (The Netherlands) _/ _/ _/ _/ _/_/_/_/
> J. Kerssens _/ _/_/ _/ _/
> _/ _/ _/_/_/_/ _/_/_/_/
> -----------------------------------------------------------------------
Hank Robinson
Oracle DBA
University of Warwick
Received on Fri Sep 02 1994 - 13:17:00 CEST