If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Possible to have a subquery within a JOIN statement?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-11, 01:42
Artemis91 Artemis91 is offline
Registered User
 
Join Date: May 2011
Posts: 4
Possible to have a subquery within a JOIN statement?

so i was wondering if there's anyone who can help me sort something out.

i have an SQL query that should show the details of a person (a search feature) that has a subquery inside an Inner Join, and the subquery is a Select statement.

Currently there's no problem with the sql query, but when i test the query as db2 i have this error:

An ON clause associated with a JOIN operator or in a MERGE statement is not valid.. SQLCODE=-338, SQLSTATE=42972, DRIVER=4.11.69

So, I'm asking, are there some ways to rewrite the query without having to use a subquery within the join statement?

I'm using DB2 For Linux, UNIX and Windows and v9.7

enlighten me
Reply With Quote
  #2 (permalink)  
Old 05-10-11, 03:52
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Show exact statement(or all of FROM clause) and all text of error message which you received.
.
Reply With Quote
  #3 (permalink)  
Old 05-10-11, 04:08
Artemis91 Artemis91 is offline
Registered User
 
Join Date: May 2011
Posts: 4
basically, here's the query:

SELECT DISTINCT p.id, p.lname, p.fname, e.no from table1 AS p
INNER JOIN table2 as e ON e.no=(SELECT no FROM table3 AS f
WHERE p.id=f.id AND f.isExit=0
AND f.pstatus <> 'cancelled'
AND f.status NOT IN ('deleted','hidden','inactive','void')
ORDER BY f.edate DESC FETCH FIRST 1 ROWS ONLY))

WHERE ...

so the problem is, the select statement in the Inner Join Statement
Reply With Quote
  #4 (permalink)  
Old 05-10-11, 04:55
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Join the subquery with a keyword LATERAL(TABLE can be specified in place of LATERAL.), like this...

Code:
SELECT DISTINCT
       p.id
     , p.lname
     , p.fname
     , e.no
 from  table1 AS p
 CROSS JOIN
       LATERAL
       (SELECT no
         FROM  table3 AS f 
         WHERE p.id = f.id
           AND f.isExit = 0 
           AND f.pstatus <> 'cancelled' 
           AND f.status NOT IN ('deleted','hidden','inactive','void') 
         ORDER BY
               f.edate DESC
         FETCH FIRST 1 ROWS ONLY
       ) f
 INNER JOIN
       table2 as e
   ON  e.no = f.no

 WHERE ...

Last edited by tonkuma; 05-10-11 at 05:00.
Reply With Quote
  #5 (permalink)  
Old 05-10-11, 05:45
Artemis91 Artemis91 is offline
Registered User
 
Join Date: May 2011
Posts: 4
thanks lots tonkuma you're a lifesaver
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On