Home » SQL & PL/SQL » SQL & PL/SQL » Rewriting a sql with NOT to avoid the NOT and OR clause (Oracle 11.2.0.5)
Rewriting a sql with NOT to avoid the NOT and OR clause [message #640251] Sat, 25 July 2015 11:19 Go to next message
orausern
Messages: 826
Registered: December 2005
Senior Member
Hi,

I am trying to rewrite a sql having a not and a OR clause to avoid both the OR and NOT clauses. Can someone help on it...it is more like an attempt to first have a rewrite and then to validate if that performs better. But the first thing is to have the rewrite done. To mimic the big sql that I have to rewrite into a simple but equally representative testcase, I have the following one:

 SELECT *
    FROM emp
   WHERE NOT (   (deptno = 10 AND ename = 'KING')
              OR (DEPTNO = 20 AND ENAME = 'SMITH'))
ORDER BY 2

Can somneone help on suggesting how to best rewrite this to avoid both NOT and the OR...the one way I could think of is as follows but I think there may be better ways to accomplish such rewrite. 

SELECT * FROM emp
MINUS
( SELECT *
   FROM emp
  WHERE (deptno = 10 AND ename = 'KING')   
 union 
 select * from emp
   WHERE
   (DEPTNO = 20 AND ENAME = 'SMITH')
   )
ORDER BY 2


Thanks,
OrauserN

[Updated on: Sat, 25 July 2015 11:22]

Report message to a moderator

Re: Rewriting a sql with NOT to avoid the NOT and OR clause [message #640252 is a reply to message #640251] Sat, 25 July 2015 12:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68644
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

NOT (   (deptno = 10 AND ename = 'KING')
              OR (DEPTNO = 20 AND ENAME = 'SMITH'))

<=>
    (ENAME != 'KING' OR DEPTNO != 10)
AND (ENAME != 'SMITH' OR DEPTNO != 20)

Re: Rewriting a sql with NOT to avoid the NOT and OR clause [message #640253 is a reply to message #640252] Sat, 25 July 2015 12:45 Go to previous messageGo to next message
orausern
Messages: 826
Registered: December 2005
Senior Member
Thanks Mike.

Agree on your point!! I had thought that the are equivaluten and found the fact the hard way (while testing)!
Re: Rewriting a sql with NOT to avoid the NOT and OR clause [message #640254 is a reply to message #640253] Sat, 25 July 2015 14:27 Go to previous message
Michel Cadot
Messages: 68644
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that writing the test your way or mine does not change anything to the performances.

Previous Topic: Transpose columns
Next Topic: ORA-01732: data manipulation operation not legal on this view
Goto Forum:
  


Current Time: Wed Apr 24 06:50:50 CDT 2024