Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.tools -> Re: SQL puzzle
As an Oracle consultant we had here explained, for a similar situation (a tuning question):
The SQL statement is parsed and pushed onto a stack. When the optimizer evaluates the code, it pulls the code back off the stack (which will be from end to start).
This might expaain why (it seems) ver. 1 works and ver. 2 doesn't (assuming that the code is also executed off the stack).
Even if this isn't the CORRECT reason, it sure sounds good (IMHO:-).
In article <94krmc$hoo$1_at_nnrp1.deja.com>,
Alex Filonov <afilonov_at_pro-ns.net> wrote:
> Looks like there is one Oracle feature which is playing it's role
here:
> Oracle parses statements from the end to the beginning. So when you
> have >0 comparison before division, it fails, when you have it before
> division, it works. I am not sure it's always so, though, so I
wouldn't
> write sql statements counting on it. I'd use comparison like:
> decode(CH2BN_MEDIAN,0,CH2IN_MEAN/CH2BN_MEDIAN)>= 2. It will work in
case
> you are using OR operator. In case of OR parsing is more complicated,
> Oracle doesn't use straitforward (or should I say backward) down-to-
top
> parsing.
>
> In article <94kmvb$p6l$1_at_nntp.Stanford.EDU>,
> gail_at_alberich.Stanford.EDU (Gail Binkley) wrote:
> > Hi all,
> >
> > We've stumbled across a SQL puzzle that I was hoping that someone
from
> > the news group could clarify. Basically, we don't understand why
one
> > query works and another doesn't.
> >
> > Statement 1 works:
> >
> > SELECT LOG_RAT2N_MEAN, SPOT
> > FROM RESULT
> > WHERE EXPTID = 918
> > and (
> > (CH2IN_MEAN/CH2BN_MEDIAN >= 2 AND CH2BN_MEDIAN > 0)
> > AND
> > (CH1I_MEAN/CH1B_MEDIAN >= 2 AND CH1B_MEDIAN > 0)
> > )
> > and LOG_RAT2N_MEAN IS NOT NULL;
> >
> > Statement 2 fails with a divison error:
> >
> > SELECT LOG_RAT2N_MEAN, SPOT
> > FROM RESULT
> > WHERE EXPTID = 918
> > and (
> > (CH2BN_MEDIAN > 0 AND CH2IN_MEAN/CH2BN_MEDIAN >= 2)
> > AND
> > (CH1B_MEDIAN > 0 AND CH1I_MEAN/CH1B_MEDIAN >= 2)
> > )
> > and LOG_RAT2N_MEAN IS NOT NULL;
> >
> > ERROR:
> > ORA-01476: divisor is equal to zero
> >
> > The data is such that there are no 0 values for CH2BN_MEDIAN, but
> > there are 0 values for CH1B_MEDIAN . It is as if the CH1B_MEDIAN >
0
> > clause in the query 2 is ignored. Both queries give the same
> > explain plan:
> >
> > QUERY_PLAN
> >
Sent via Deja.com
http://www.deja.com/
Received on Fri Jan 26 2001 - 09:15:55 CST
![]() |
![]() |