A little more explanation... The point of the query was to retrieve a subset of Event rows for any premise where there existed 2 similar 'marker' events (EVTRMK LIKE 'CIS CONTRIB. ACCT #%'). The return set I wanted would be the 2 marker rows plus all rows created between the 1st and 2nd marker.
I could not get the first suggested SQL to work, the join/group by/having one. But it didn't look like it would return the rows I wanted.
The second suggestion, the one replacing the OR with a UNION, worked perfectly... and took about 90 seconds to run. Thank you Mr. Morgan, I will keep this in mind for future queries!
Posted for Michael Palazzolo ... SQL Developer NW Natural
by
Kenneth E. Graap
http://www.linkedin.com/in/kennethgraap
-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Morgan, Paul
Sent: Wednesday, June 08, 2011 6:42 AM
To: Midrange Systems Technical Discussion
Subject: RE: Problem using OR in SQL Statement
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
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at
http://archive.midrange.com/midrange-l.
As an Amazon Associate we earn from qualifying purchases.