Home » SQL & PL/SQL » SQL & PL/SQL » How avoid More than one Join With Same table (merged) (Oracle, Windows 10)
How avoid More than one Join With Same table (merged) [message #690031] Fri, 20 September 2024 11:18 Go to next message
Nicha
Messages: 34
Registered: March 2020
Member
Hi,

I have two tables [Customers] and [Accounts]. The [Customers] table has the information about the Customers, while the [Accounts] table has all the accounts where they Intervene.

You can see both tables, and the desired result below:

/forum/fa/14817/0/


I am using the Query:

Select  A.Client_ID, A.Name, B.Account, B.Interv_Order,
        --Cli_Interv. Data
        Cli_Interv.Cliente_ID as Cli_Interv, Cli_Interv.Name as Name_Interv, 
        Cli_Interv.Interv_Order as Interv_Ord, Cli_Interv.Start_Date

From    Customers A,
        Accounts B,

        --View to get Names for all Customers on an Account.
        (Select A.Account, A.Cliente_ID, B.Name, A.Interv_Order, A.Start_Date
            
            From    Accounts A,
                    Customers B
                    
         where A.Client_ID = B.Client_ID           
        
        ) Cli_Interv
        
where   A.Client_ID = B.Client_ID and
        B.Account = Cli_Interv.Account (+)
        
Order by A.Client_ID, B.Account
 


Tables Data:
CREATE TABLE Customers
( 
  Client_ID	Number			NOT NULL,
  Name     	VARCHAR2(30)            NOT NULL
)

insert into Customer values (10,'Olivia Newton-john');
insert into Customer values (11,'Noah Cyrus');
insert into Customer values (12,'Oliver Twist');
insert into Customer values (13,'Sophia Loren');
insert into Customer values (14,'Abby Elliot');
insert into Customer values (15,'Alex Moraes');


CREATE TABLE Accounts
( 
  Account       VARCHAR2(2)            	NOT NULL,
  Client_ID	Number			NOT NULL,
  Interv_Order  Number			NOT NULL,
  Start_Date    DATE                    NOT NULL
)

insert into Accounts values ('A1',10,1,To_date('2024-02-13','YYYY-MM-DD'));
insert into Accounts values ('A2',14,1,To_date('2024-05-20','YYYY-MM-DD'));
insert into Accounts values ('A2',11,2,To_date('2024-05-20','YYYY-MM-DD'));
insert into Accounts values ('A2',15,3,To_date('2024-05-20','YYYY-MM-DD'));
insert into Accounts values ('A3',12,1,To_date('2024-08-05','YYYY-MM-DD'));
insert into Accounts values ('A4',11,1,To_date('2014-09-02','YYYY-MM-DD'));

The problem is that to get the data of the Customers, who intervene in an account, I am going, a second time, to the same tables with the View [Cli_Interv].

Is there any other way to get the same result without having to use the View [Cli_Interv]?

Thank you in advance.
Re: How avoid More than one Join With Same table [message #690033 is a reply to message #690031] Fri, 20 September 2024 14:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68693
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

A4 is missing in your result, is there a specific reason?

SQL> col name format a18
SQL> col name_interv format a18
SQL> break on client_id dup skip 1
SQL> select a.client_id, a.name,
  2         b.account, b.interv_order,
  3         c.client_id cli_invert, d.name name_interv, c.interv_order, c.start_date
  4  from customers a
  5       left outer join accounts b on b.client_id = a.client_id
  6       left outer join accounts c on c.account = b.account 
  7       left outer join customers d on d.client_id = c.client_id
  8  order by a.client_id, b.account, c.interv_order
  9  /
 CLIENT_ID NAME               AC INTERV_ORDER CLI_INVERT NAME_INTERV        INTERV_ORDER START_DATE
---------- ------------------ -- ------------ ---------- ------------------ ------------ -----------
        10 Olivia Newton-john A1            1         10 Olivia Newton-john            1 13-FEB-2024

        11 Noah Cyrus         A2            2         14 Abby Elliot                   1 20-MAY-2024
        11 Noah Cyrus         A2            2         11 Noah Cyrus                    2 20-MAY-2024
        11 Noah Cyrus         A2            2         15 Alex Moraes                   3 20-MAY-2024
        11 Noah Cyrus         A4            1         11 Noah Cyrus                    1 02-SEP-2014

        12 Oliver Twist       A3            1         12 Oliver Twist                  1 05-AUG-2024

        13 Sophia Loren

        14 Abby Elliot        A2            1         14 Abby Elliot                   1 20-MAY-2024
        14 Abby Elliot        A2            1         11 Noah Cyrus                    2 20-MAY-2024
        14 Abby Elliot        A2            1         15 Alex Moraes                   3 20-MAY-2024

        15 Alex Moraes        A2            3         14 Abby Elliot                   1 20-MAY-2024
        15 Alex Moraes        A2            3         11 Noah Cyrus                    2 20-MAY-2024
        15 Alex Moraes        A2            3         15 Alex Moraes                   3 20-MAY-2024

[Updated on: Fri, 20 September 2024 14:31]

Report message to a moderator

Re: How avoid More than one Join With Same table [message #690034 is a reply to message #690033] Fri, 20 September 2024 18:12 Go to previous messageGo to next message
Nicha
Messages: 34
Registered: March 2020
Member
Thank you @Michel Cadot.
Regarding the A4 account, I forgot to include on my output. Forguive me, please.
Could you please explain how your query works, and if has better performance than the one I presented?

Isn't your solution calling the tables more than once, as I expect that it was possible to build a query that coul avoid that?

I'm no expert, but if this is the best solution, can you explain how it work's, and if the outer joins are calling the same tables more than once, doesn't that compromise performance?

Many thanks and my best regards for your precious help.
Re: How avoid More than one Join With Same table [message #690035 is a reply to message #690034] Sat, 21 September 2024 00:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68693
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You cannot avoid to reach twice each table because this is needed by the selected values.
The query is simply built from these later ones; first table values then second ones then...
The outer joins are required because not all customers have accounts.

Re: How avoid More than one Join With Same table [message #690036 is a reply to message #690035] Mon, 23 September 2024 04:53 Go to previous messageGo to next message
cookiemonster
Messages: 13943
Registered: September 2008
Location: Rainy Manchester
Senior Member
@Nicha - If you run your query (once you've fixed the typos) it leaves out Sophia Loren, and that happens because you didn't outer join Accounts A.
You should have done this:
Select  A.Client_ID, A.Name, B.Account, B.Interv_Order,
        --Cli_Interv. Data
        Cli_Interv.Client_ID as Cli_Interv, Cli_Interv.Name as Name_Interv, 
        Cli_Interv.Interv_Order as Interv_Ord, Cli_Interv.Start_Date
From    Customers A,
        Accounts B,
        --View to get Names for all Customers on an Account.
        (Select A.Account, A.Client_ID, B.Name, A.Interv_Order, A.Start_Date
            From    Accounts A,
                    Customers B
         where A.Client_ID = B.Client_ID           
        ) Cli_Interv
where   A.Client_ID = B.Client_ID (+) and
        B.Account = Cli_Interv.Account (+)
Order by A.Client_ID, B.Account
Your requirement appears to be:
a) Get all customers no matter what.
b) For each customer get all the accounts they are on - so outer join to accounts as they may not have any (Sophia)
c) For each account they are on get all the accounts entries for that account - so outer join accounts to itself, can't be inner as that would make b) inner
d) For each account entry get the name of the customer - so outer join back to customers - so outer join accounts back to customers, again can't be inner as that would make c) inner.

You tried to skip one of the outer joins you needed by doing an inline view but really that's functionally the same as Michel's version and oracle may well get the data in the same way.

You're worried about performance, but when dealing with outer joins the first question is - does the functionality require them? Your functionality does.

So do you have a performance issue, or are you just worried you'll get one because you're using outer-joins?

If you do have an issue feel free to post details here and we'll see if we can help. If you don't know, check, because Michel's query may run just fine.
icon14.gif  Re: How avoid More than one Join With Same table [message #690037 is a reply to message #690036] Mon, 23 September 2024 05:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68693
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

/forum/fa/2115/0/

I'd like to better know English to explain the issue as well as cookiemonster's post.

Re: How avoid More than one Join With Same table [message #690038 is a reply to message #690037] Mon, 23 September 2024 07:41 Go to previous messageGo to next message
cookiemonster
Messages: 13943
Registered: September 2008
Location: Rainy Manchester
Senior Member
ton anglais est bien meilleur que mon français


[yes I used google translate, my french isn't even up to that, as I've hardly used it since school. If I'm not asking for directions to the swimming pool I'm pretty stuck]
Re: How avoid More than one Join With Same table [message #690043 is a reply to message #690038] Wed, 25 September 2024 11:07 Go to previous message
Nicha
Messages: 34
Registered: March 2020
Member
hi both @Michel Cadot and @cookiemonster

Many thank's to you Both. I'm from Portugal, so please don't worry, caus my english worse than yours Laughing

I've understood both your explanation.

My best regards.
Previous Topic: PLS-00172: string literal too long while passing large XML as input to a procedure
Next Topic: SQL Macro using UTL_FILE
Goto Forum:
  


Current Time: Sat Sep 28 01:13:42 CDT 2024