Re: factorial

From: Fan Ruo Xin <fanruox_at_yahoo.com>
Date: Sun, 06 Jan 2002 17:35:13 GMT
Message-ID: <3C388AFC.F115B1EA_at_yahoo.com>


Mikito Harakiri wrote:

> Could it be reduced to just
>
> with factorial (n, f) as
> ( values(1, 1)
> union all
> select n+1, f*(n+1)
> from factorial
> where n<10
> )

No, you can't!

>
>
> ? Then, could the engine push the safety predicate (n<10) from outer query like
> this:
>
> with factorial (n, f) as
> ( values(1, 1)
> union all
> select n+1, f*(n+1)
> from factorial
> )
> select * from factorial where n<10
>

No!
I use the level to control the depth.

>
> ? (I guess not, as this is essentially my query:-(
>
> If it cant push "n<10", then can it at least push "level<10":
>
> with factorial (level, n, f) as
> ( values(1, 1, 1)
> union all
> select level+1, n+1, f*(n+1)
> from factorial
> )
> select * from factorial where level<10;
>
> ?
>
> In article <3C37DB75.6AC77849_at_yahoo.com>, Fan Ruo Xin says...
> >
> >
> >try this:
> >with factorial (level, n, f) as
> > ( values(1, 1, 1)
> > union all
> > select level+1, n+1, f*(n+1)
> > from factorial
> > where level<10
> > )
> >select * from factorial;
> >
> >Just wonder why use sql to do this kind of thing?
> >Regards,
> >
> >Fan Ruo Xin
> >
> >
> >Mikito Harakiri wrote:
> >
> >> WITH
> >> factorial AS (
> >> select n+1, f*(n+1) from factorial
> >> union
> >> select 1, 1 from table(values(1,1)) )
> >> SELECT f FROM factorial
> >> WHERE n=5
> >>
> >> 1. Is this going to work?
> >> 2. If not why, and what would be the correct recursive definition?
> >
Received on Sun Jan 06 2002 - 18:35:13 CET

Original text of this message