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 > problem with JOIN

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-22-10, 09:31
archie.by archie.by is offline
Registered User
 
Join Date: Apr 2010
Posts: 7
problem with JOIN

Hello
I'd like to perform the following db request

SELECT
outlook.*,
mrWED.WEEK_ENDING_DATE
FROM
CLAIMVW.OUTLOOK outlook

LEFT JOIN (SELECT MAX(WEEK_ENDING_DATE) as WEEK_ENDING_DATE, EMP_SER_NUM, EMP_COMPANY_CODE, COUNTRY_CODE, WORK_ITEM_ID, GROUP_ID FROM CLAIMVW.OUTLOOK WHERE WEEK_ENDING_DATE<='2010-04-22' AND WEEK_ENDING_DATE>outlook.WEEK_ENDING_DATE GROUP BY EMP_SER_NUM, EMP_COMPANY_CODE, COUNTRY_CODE, WORK_ITEM_ID, GROUP_ID) mrWED ON
mrWED.EMP_SER_NUM = outlook.EMP_SER_NUM AND
mrWED.EMP_COMPANY_CODE = outlook.EMP_COMPANY_CODE AND
mrWED.COUNTRY_CODE = outlook.COUNTRY_CODE AND
mrWED.WORK_ITEM_ID = outlook.WORK_ITEM_ID AND
mrWED.GROUP_ID = outlook.GROUP_ID

But the problem is that I can't use outlook.WEEK_ENDING_DATE in the inner SELECT clause. I'm retrieving the following ERROR:
[IBM][CLI Driver][DB2] SQL0206N "OUTLOOK.WEEK_ENDING_DATE" is not valid in the context where it is used. SQLSTATE=42703

is there any workaround that will let me get the results I'm expecting?

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 04-22-10, 10:02
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
TABLE keyword might be a trick.

LEFT JOIN TABLE(SELECT ...
Reply With Quote
  #3 (permalink)  
Old 04-22-10, 10:17
archie.by archie.by is offline
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks tonkuma
It is a trick
But after I added TABLE performance decreased by more than 10 times.
I hope there must be some other way out
Reply With Quote
  #4 (permalink)  
Old 04-22-10, 10:33
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Here are two examples.
Some performance improvements may be possible for these queries.

Code:
SELECT 
       outlook.*
     , mrWED.WEEK_ENDING_DATE
  FROM 
       CLAIMVW.OUTLOOK outlook
  LEFT JOIN
       TABLE
       (SELECT MAX(WEEK_ENDING_DATE) as WEEK_ENDING_DATE
             , EMP_SER_NUM
             , EMP_COMPANY_CODE
             , COUNTRY_CODE
             , WORK_ITEM_ID
             , GROUP_ID
          FROM CLAIMVW.OUTLOOK mrWED
         WHERE mrWED.WEEK_ENDING_DATE <= '2010-04-22'
           AND mrWED.WEEK_ENDING_DATE >  outlook.WEEK_ENDING_DATE
           AND mrWED.EMP_SER_NUM      =  outlook.EMP_SER_NUM
           AND mrWED.EMP_COMPANY_CODE =  outlook.EMP_COMPANY_CODE
           AND mrWED.COUNTRY_CODE     =  outlook.COUNTRY_CODE
           AND mrWED.WORK_ITEM_ID     =  outlook.WORK_ITEM_ID
           AND mrWED.GROUP_ID         =  outlook.GROUP_ID
       ) mrWED
   ON  0 = 0
;

Code:
SELECT 
       outlook.*
     , (SELECT MAX(WEEK_ENDING_DATE) as WEEK_ENDING_DATE
             , EMP_SER_NUM
             , EMP_COMPANY_CODE
             , COUNTRY_CODE
             , WORK_ITEM_ID
             , GROUP_ID
          FROM CLAIMVW.OUTLOOK mrWED
         WHERE mrWED.WEEK_ENDING_DATE <= '2010-04-22'
           AND mrWED.WEEK_ENDING_DATE >  outlook.WEEK_ENDING_DATE
           AND mrWED.EMP_SER_NUM      =  outlook.EMP_SER_NUM
           AND mrWED.EMP_COMPANY_CODE =  outlook.EMP_COMPANY_CODE
           AND mrWED.COUNTRY_CODE     =  outlook.COUNTRY_CODE
           AND mrWED.WORK_ITEM_ID     =  outlook.WORK_ITEM_ID
           AND mrWED.GROUP_ID         =  outlook.GROUP_ID
       )
  FROM 
       CLAIMVW.OUTLOOK outlook
;
Reply With Quote
  #5 (permalink)  
Old 04-22-10, 10:56
archie.by archie.by is offline
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks tonkuma
Reply With Quote
  #6 (permalink)  
Old 04-22-10, 11:17
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Another solution

How you shown your original query JOIN TABLE is required. Tonkuma gave you right direction. But another solution also possible:

Code:
SELECT 
outlook.*,
mrWED.WEEK_ENDING_DATE max_WEEK_ENDING_DATE
FROM 
CLAIMVW.OUTLOOK outlook

left JOIN 
(SELECT MAX(u1.WEEK_ENDING_DATE) as WEEK_ENDING_DATE, 
u1.EMP_SER_NUM, u1.EMP_COMPANY_CODE, u1.COUNTRY_CODE, 
u1.WORK_ITEM_ID, u1.GROUP_ID 
FROM 
CLAIMVW.OUTLOOK u1, CLAIMVW.OUTLOOK u2
WHERE 
u1.EMP_SER_NUM = u2.EMP_SER_NUM 
AND 
u1.EMP_COMPANY_CODE = u2.EMP_COMPANY_CODE 
AND 
u1.COUNTRY_CODE = u2.COUNTRY_CODE 
AND 
u1.WORK_ITEM_ID  = u2.WORK_ITEM_ID 
AND 
u1.GROUP_ID = u2.GROUP_ID
and   
u1.WEEK_ENDING_DATE <= '2010-04-22' 
AND   
u1.WEEK_ENDING_DATE  > u2.WEEK_ENDING_DATE 
GROUP BY u1.EMP_SER_NUM, u1.EMP_COMPANY_CODE, 
u1.COUNTRY_CODE, u1.WORK_ITEM_ID, u1.GROUP_ID) mrWED 

ON 
mrWED.EMP_SER_NUM = outlook.EMP_SER_NUM 
AND 
mrWED.EMP_COMPANY_CODE = outlook.EMP_COMPANY_CODE 
AND 
mrWED.COUNTRY_CODE = outlook.COUNTRY_CODE 
AND 
mrWED.WORK_ITEM_ID = outlook.WORK_ITEM_ID 
AND 
mrWED.GROUP_ID = outlook.GROUP_ID
Lenny
Reply With Quote
  #7 (permalink)  
Old 04-22-10, 13:33
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
How did you come up with the 10x slower if you original query had a syntax problem? What are you comparing?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 04-23-10, 04:36
archie.by archie.by is offline
Registered User
 
Join Date: Apr 2010
Posts: 7
I executed the query without the condition highlighted in blue.
It took about 2 secs to execute, while TABLE decorated query was executing for about 25 secs.

It was not a big deal to understand that smth was going wrong.
Reply With Quote
  #9 (permalink)  
Old 04-23-10, 04:54
archie.by archie.by is offline
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks for your solution Lenny
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