Compute 10! using Recursive WITH clause



-- 
; with refc as (

select 1 as n, 1 as running_factorial
union all
select r.n+1, running_factorial*b.num from refc r 
inner join  (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) b(num)  on r.n+1=b.num )

 
select n, running_factorial from refc


--******RunningProduct

SELECT n, ROUND(EXP(SUM(LOG(n)) OVER  (ORDER BY n)), 0)  as runningProduct
FROM (Values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) d(n)

--*******This one is clower
 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s