Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Need information on UNION and JOIN in DB2

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
sureshbabu.jv

New User


Joined: 11 Apr 2006
Posts: 41
Location: Chennai

PostPosted: Fri Dec 22, 2006 11:52 am    Post subject: Need information on UNION and JOIN in DB2
Reply with quote

Hi,

I used the below query in my program, the job has been take much time to get completed that's why it's creating problem in production. I used UNION in the query to add rows, I heard that JOIN can take less time than UNION but I am not getting an idea to rewrite this query using JOIN clause instead of UNION.

Could you please give me suggestion to reqrite the below query using join clause. This is very important forme..

EXEC SQL DECLARE TIVAPSUM CURSOR FOR
SELECT
:BLANK-SUMMARY-IND,
APMST.REMIT_VEND_CODE,
APMST.REMIT_VEND_SUFFIX,
APMST.COMPANY,
APMST.EMPLOYEE_NUMBER,
APMST.BANK_ID,
APMST.BANK_ACCOUNT,
APMST.PAYMENT_STATUS,
APMST.INVOICE_STATUS,
APMST.PAYMENT_DATE,
APMST.PAYMT_REF_NO,
:PAYMENT-ALL-LINE-GLIT,
APMST.PAYMENT_AMT,
APMST.PAYMENT_AMT_FX,
APMST.INV_AMT_WITHHELD,
APMST.CURRENCY_CODE,
APMST.CURRENCY_CODE_FX,
APMST.CURRENCY_RATE_FROM,
APMST.PAYMENT_METHOD
FROM TIDAPMST APMST
WHERE
(APMST.INVOICE_TYPE NOT IN
(
:MODEL-7-LITRL,
:DEPOSIT-6-LITRL,
:WITHDRAW-8-LITRL))
AND ( ( (APMST.PAYMENT_STATUS IN
(
:APPROVED-28-LITRL,
:HOLD-29-LITRL,
:PAID-30-LITRL))
AND (APMST.INVOICE_STATUS IN
(
:ACCEPTED-2-LITRL,
:MISMATCH-4-LITRL,
:COMPLETE-3-LITRL)))

OR ( (APMST.INVOICE_STATUS = :MISMATCH-4-LITRL)

AND (APMST.PAYMENT_STATUS =
:APPROVED-28-LITRL))

OR ( (APMST.PAYMENT_STATUS IN
(
:PENDING-26-LITRL,
:REJECT-31-LITRL,
:REVIEW-32-LITRL))

AND (APMST.INVOICE_STATUS =
:ACCEPTED-2-LITRL)))

AND NOT EXISTS
(
SELECT
APLIN.PAYMT_REF_NO
FROM TIDAPLIN APLIN
WHERE
(APLIN.PAYMT_REF_NO =
APMST.PAYMT_REF_NO)
AND (APLIN.SUBLINE_LINE ?> :BLANK-SUBLINE-APCOM)
AND (((APLIN.LINE_PAY_STATUS ?=
APMST.PAYMENT_STATUS) AND
((APLIN.INVOICE_LINE ?=
:WITHHELD-LINE-APCOM ) OR
(APLIN.LINE_PAY_STATUS ?= :HOLD-10-LITRL )))
OR (APLIN.INVOICE_LINE_STAT ?=
APMST.INVOICE_STATUS))
)

UNION

SELECT
:BLANK-SUMMARY-IND,
APMST.REMIT_VEND_CODE,
APMST.REMIT_VEND_SUFFIX,
APMST.COMPANY,
APMST.EMPLOYEE_NUMBER,
APMST.BANK_ID,
APMST.BANK_ACCOUNT,
APLIN.LINE_PAY_STATUS,
APLIN.INVOICE_LINE_STAT,
APMST.PAYMENT_DATE,
APMST.PAYMT_REF_NO,
APLIN.INVOICE_LINE,
APLIN.PAYMENT_AMT,
APLIN.PAYMENT_AMT_FX,
:ZVAL-INV-AMT-WITHHELD,
APLIN.CURRENCY_CODE,
APLIN.CURRENCY_CODE_FX,
APLIN.CURRENCY_RATE_FROM,
APMST.PAYMENT_METHOD
FROM TIDAPMST APMST,
TIDAPLIN APLIN
WHERE
(APMST.PAYMT_REF_NO =
APLIN.PAYMT_REF_NO) AND
(APMST.INVOICE_TYPE NOT IN
(
:MODEL-7-LITRL,
:DEPOSIT-6-LITRL,
:WITHDRAW-8-LITRL)) AND
(APLIN.SUBLINE_LINE ?> :BLANK-SUBLINE-APCOM) AND

(((APLIN.LINE_PAY_STATUS IN
(
:APPROVED-9-LITRL,
:HOLD-10-LITRL,
:PAID-11-LITRL)) AND
(APLIN.INVOICE_LINE_STAT IN
(
:ACCEPTED-23-LITRL,
:MISMATCH-25-LITRL,
:COMPLETE-24-LITRL))) OR

((APLIN.INVOICE_LINE_STAT =
:MISMATCH-25-LITRL) AND
(APLIN.LINE_PAY_STATUS =
:APPROVED-9-LITRL)) OR

((APLIN.LINE_PAY_STATUS IN
(
:PENDING-27-LITRL,
:REJECT-12-LITRL,
:REVIEW-13-LITRL)) AND
(APLIN.INVOICE_LINE_STAT = :ACCEPTED-23-LITRL)))

AND EXISTS
(
SELECT
APLI1.PAYMT_REF_NO
FROM TIDAPLIN APLI1
WHERE
(APLI1.PAYMT_REF_NO =
APMST.PAYMT_REF_NO)
AND (APLI1.SUBLINE_LINE ?> :BLANK-SUBLINE-APCOM)
AND (((APLI1.LINE_PAY_STATUS ?=
APMST.PAYMENT_STATUS) AND
((APLI1.INVOICE_LINE ?=
:WITHHELD-LINE-APCOM ) OR
(APLI1.LINE_PAY_STATUS ?= :HOLD-10-LITRL )))
OR (APLI1.INVOICE_LINE_STAT ?=
APMST.INVOICE_STATUS))
)

END-EXEC.


Thanks,
Suresh
Back to top
View user's profile Send private message

DavidatK

Active Member


Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

PostPosted: Fri Dec 22, 2006 11:13 pm    Post subject: Re: Need information on UNION and JOIN in DB2
Reply with quote

Hi suresh,

UNIONs and JOINs are completely different in function and are not interchangeable.

Check this link to a prior post, I think member ?guptae? has a good explanation.

PRIOR POST
Back to top
View user's profile Send private message
joshkahn

New User


Joined: 30 Dec 2005
Posts: 8

PostPosted: Thu Dec 28, 2006 3:12 am    Post subject:
Reply with quote

Using host variables in a sub-select makes what is called a correlated subselect. That function is always slow.

Might be better off using a simple select and selecting data with program logic.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Information About the Equifax Securit... Rohit Umarjikar PC Guides & IT News 6 Mon Sep 11, 2017 9:10 pm
No new posts Join records from 2 files with No Dup... Poha Eater DFSORT/ICETOOL 22 Sun Aug 27, 2017 10:35 pm
No new posts retrieving envelope information out o... VintageReceiver TSO/ISPF 6 Thu Aug 10, 2017 8:13 pm
No new posts Reduce CPU Times for Join Sort santoshn SYNCSORT 12 Sat Jun 10, 2017 1:40 pm
No new posts Join giving more records than expected Danielle.Filteau SYNCSORT 2 Thu Mar 09, 2017 3:28 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us