Finishing up on what Dan suggested replacing the OR with a UNION. No ALL on the union which would create duplicate rows and adding in the third exists:
SELECT E.PRMNBR, E.EVTDTE, E.EVTTME, E.USRID, E.EVTRMK
FROM P1FILES.KSEVT E
WHERE EVTRMK LIKE 'CIS CONTRIB. ACCT #%'
AND EXISTS( SELECT *
FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR
AND EVTID <> E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%' )
UNION
SELECT E.PRMNBR, E.EVTDTE, E.EVTTME, E.USRID, E.EVTRMK
FROM P1FILES.KSEVT E
WHERE EXISTS( SELECT *
FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR
AND EVTID > E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%' )
AND EXISTS( SELECT *
FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR
AND EVTID < E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%' )
I'd be curious if this also hangs...
Paul Morgan
Principal Programmer Analyst
IT Supply Chain/Replenishment
-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Graap, Kenneth
Sent: Tuesday, June 07, 2011 4:02 PM
To: Midrange Systems Technical Discussion (midrange-l@xxxxxxxxxxxx)
Subject: Problem using OR in SQL Statement
This SQL select statement containing an 'OR' within the Where clause will not run to completion, hangs until cancelled...
SELECT PRMNBR, EVTDTE, EVTTME, USRID, EVTRMK
FROM P1FILES.KSEVT E
WHERE
(EVTRMK LIKE 'CIS CONTRIB. ACCT #%'
AND EXISTS(SELECT * FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR AND EVTID <> E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%'))
OR
(EXISTS(SELECT * FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR AND EVTID > E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%')
AND
EXISTS(SELECT * FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR AND EVTID < E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%'))
However, breaking the statement above apart and running the two parts separately does work...
Using the statement containing only the first portion of the Where clause, left of the 'OR', returns 390 rows...
SELECT PRMNBR, EVTDTE, EVTTME, USRID, EVTRMK
FROM P1FILES.KSEVT E
WHERE
(EVTRMK LIKE 'CIS CONTRIB. ACCT #%'
AND EXISTS(SELECT * FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR AND EVTID <> E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%'))
Using the statement containing only the second portion of the Where clause, right of the 'OR', returns 1738 rows...
SELECT PRMNBR, EVTDTE, EVTTME, USRID, EVTRMK
FROM P1FILES.KSEVT E
WHERE
(EVTRMK LIKE 'CIS CONTRIB. ACCT #%'
AND EXISTS(SELECT * FROM P1FILES.KSEVT
WHERE PRMNBR = E.PRMNBR AND EVTID <> E.EVTID
AND EVTRMK LIKE 'CIS CONTRIB. ACCT #%'))
Does anyone know why this won't work with the OR ???????
This is being run on a V5R4M5 system...
[Description: Description: nwnatural]
Kenneth E. Graap | Systems Administrator
keg@xxxxxxxxxxxxx<mailto:keg@xxxxxxxxxxxxx> 503-226-4211 x5537
http://www.linkedin.com/in/kennethgraap
As an Amazon Associate we earn from qualifying purchases.