Solved

where  table1.colA(+) <= table2.colB

Posted on 2016-09-21
7
31 Views
Last Modified: 2016-10-12
I am trying to read some sql code that I have been given, but I am having a bit of trouble trying to understand what the following means

where  table1.colA(+) <= table2.colB


I think this means the following:

i am doing an outer join of table1
        I have all possible data in table 2, but not all data in table 1

So in the where clause

When I have data in both tables and both columns,
   the where will check if colA (in table1) is less than or equal to colB (table 2) (and return true or false based on the data in the columns)

When I have no data in Table1 and data in Table2 and ColB
   the where will ignore the condition and return true, the (+) in oracle is telling the sql return true when no data.

When I have data in both Tables but the value in colA (Table1) is null
   I am not sure what the where could is doing?
might be :
     there is a row in Table1 but colA is NULL so the <= is going to be false
     and neither the row in column B or column A should return.

or:
   since no row in table 1 or null value in colA equate to the same thing in oracle
  then a row is returned because table 2 has a row and column B has a value



Finally
When I have data in both Tables but the value in both columns is null
   I am not sure what the where could is doing?
might be :
     there is a row in Table1 but colA is NULL so the <= is going to be false
     and neither the row in column B or column A should return.

or:
   since no row in table 1 or null value in colA equate to the same thing in oracle
  a is still not returned because
   table 2 has a row and colB is null
   the where clause will evaluate colb and since its null the '<=' condition will return false.



I dont have access to a database for a while otherwise i would of justed tested this. But if anyone could confirm or help me
0
Comment
Question by:jhacharya
  • 3
  • 2
7 Comments
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 41810143
Hi,
For outer join, you should not use '<=' but only

where  table1.colA(+) = table2.colB

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41810392
Thats not correct. If you need <= then you can use it in an outer or inner join condition.

operators are operators
no matter where you use them.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points (awarded by participants)
ID: 41810397
where  table1.colA(+) <= table2.colB

Can be re-written as:

From table2
Left join table1 on  table1.colA <= table2.colB

The old (+) syntax used by Oracle is used to indicate that unmatched rows can still be returned by the query (an outer join)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41811309
Here is an example :

Table gv$session has session_id (SID )  from instances (INST_ID), 1 and 2
Table gv$px_session has session_id (SID) of sessions with parallel threads running on instances (INST_ID), 1 and 2


SELECT G.INST_ID ID, G.SID SESION,p.inst_id,p.sid
  FROM gv$session G,gv$px_session p
 WHERE p.inst_id(+) = G.INST_ID
 and G.sid(+)=G.sid
 order by 3
 
     ID     SESION   INST_ID        SID
---------- ----------     ----------        ----------
1            2711          1                2711
2            1923          2                1923
2            2303          2                2303
1            1935        
1            2695        
2            1155        
1            1555        
1            28        
2            28
       
Now , (+) <= , means  all the data from right table i.e. G (gv$session) in above case and
matching data (to where clause) from left table i.e. P (gv$px_session)

SELECT G.INST_ID ID, G.SID SESION,p.inst_id,p.sid
  FROM gv$session G,gv$px_session p
 WHERE G.inst_id(+) = p.INST_ID
 and g.sid(+)=p.sid
 order by 3
 
1    2711    1    2711
2    1902    2    1902
2    2303    2    2303
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 41811623
with tableB as (
                   select 1 as id, 'Bnm1' as name from dual union all
                   select 2 as id, 'Bnm1' as name from dual union all
                   select 3 as id, 'Bnm1' as name from dual
               )
, tableA as (
                   select 2 as id, 'Anm1' as name from dual union all
                   select 3 as id, 'Anm1' as name from dual
               )
select 
      '(+) way' method, tableb.id b_id, tablea.id a_id, tableb.name b_nm, tablea.name a_nm
from tableB, tableA
where tableA.id(+) <= tableB.id


UNION ALL

select
          'ansi way', tableb.id, tablea.id, tableb.name, tablea.name
from tableB
left join tableA on tableA.id <= tableB.id

order by 1,2,3

Open in new window

result, note that for some rows of tableB there is no match in tableA but using old fashioned (+) or the ANSI standard LEFT JOIN those unmatched rows in tableB are still included in the result
|   METHOD | B_ID |   A_ID | B_NM |   A_NM |
|----------|------|--------|------|--------|
|  (+) way |    1 | (null) | Bnm1 | (null) |
|  (+) way |    2 |      2 | Bnm1 |   Anm1 |
|  (+) way |    3 |      2 | Bnm1 |   Anm1 |
|  (+) way |    3 |      3 | Bnm1 |   Anm1 |
| ansi way |    1 | (null) | Bnm1 | (null) |
| ansi way |    2 |      2 | Bnm1 |   Anm1 |
| ansi way |    3 |      2 | Bnm1 |   Anm1 |
| ansi way |    3 |      3 | Bnm1 |   Anm1 |

Open in new window

see http://sqlfiddle.com/#!4/9eecb7/7648
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 41832846
Jhacharya,
Did you try with the suggestions given in above, to your original question? Please update us. Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now