Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This challenge though does not have any resemblance with the real time problem directly, but it measures about logical thinking. The problem is all about finding the factorial of numbers

TSQL Beginners Challenge 3 - Find the Factorial

Recursive CTE - Simple Solution

Aug 6 2012 12:00AM by unglesb   

First, create a CTE with nums, cur, and factorial. Then select the num, 1 as cur and 1 as factorial.

Next you will union this with nums, cur + 1, and factorial * (cur + 1), which would give you the next level. You have to join the CTE and the nums table and on your join (or where) you want to specify that cur + 1 must be <= nums.

Finally, select nums and the MAX of the factorial column from the CTE, grouping by and ordering by the nums column.

;WITH FACT_CTE (nums, cur, factorial)
AS
(
	SELECT F.nums, 1 [cur], 1 [factorial]
	FROM @Fact F
	UNION ALL
	SELECT F.nums, cur + 1 [cur], FC.factorial * (FC.cur + 1) [factorial]
	FROM @Fact F
		JOIN FACT_CTE FC ON
			F.nums = FC.nums
			AND FC.cur + 1 <= F.nums
)
SELECT nums, MAX(factorial)
FROM FACT_CTE
GROUP BY nums
ORDER BY nums

Tags: recursive cte factorial


unglesb
457 · 0% · 83
1



Submit

Your Comment


Sign Up or Login to post a comment.

"Recursive CTE - Simple Solution" rated 5 out of 5 by 1 readers
Recursive CTE - Simple Solution , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]