这是一个关于CTE的应用,这里我们用CTE实现阶乘 Factorial,首先来看一个简单的小实验,然后再来看题目。有的童鞋会问怎么没有2就来3了呢,惭愧,TSQL Beginners Challenge 2对应的题目我没能做出来。上代码:

;with cte as
(
select num=1,fact=1 union all
select num=num+1,fact=fact*(num+1) from cte where num<5
)
select * from cte

上面的查询会有什么结果呢,大家可以粘到查询分析器里面看下,简单的实现了阶乘吧。CTE的递归是有层级限制的,写Blog的时候想不起来相关的语法结构,又懒得去查,偷个懒直接用num<5来限制下,以免报错。:)。有了这个打的,下面的题目就很容易了,现在我们来看题目吧:

Introduction

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. Though it is known to most of us what a factorial is, but to recall the concept here is an example:

Factorial of 3 is 1*2*3 = 6 i.e. the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Sample Data

1.Nums
2.-----------
3.0
4.1
5.3
6.5
7.10 

Expected Results

1.Nums        Factorial
2.----------- -----------
3.0                    1
4.1                    1
5.3                    6
6.5                  120
7.10             3628800
--创建表
CREATE TABLE [Fact](
[Nums] [int] NULL
) --构造数据
insert into Fact(Nums) values(0)
insert into Fact(Nums) values(1)
insert into Fact(Nums) values(10)
insert into Fact(Nums) values(3)
insert into Fact(Nums) values(5) --方式一
;WITH cte AS
(
SELECT num=0,factorial=1
UNION ALL
SELECT num=num+1,(num+1)*factorial FROM cte WHERE num<10
)
SELECT Nums,factorial FROM cte a, Fact b WHERE a.num=b.Nums
 

TSQL Beginners Challenge 3 - Find the Factorial的更多相关文章

  1. TSQL Beginners Challenge 1 - Find the second highest salary for each department

    很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...

  2. TSQL Challenge 2

    和之前发布的TSQL Challenge 1是同一系列的文章,看到那篇学习哪篇,没有固定的顺序,只为锻炼下思维. Compare rows in the same table and group th ...

  3. TSQL Challenge 1

    在老外网站发布的一些SQL问题,拿过来自己搞一下,后面我也会陆续转载一些问题,欢迎看到的朋友贴出自己的答案,交流一哈.对于技术问答题的描述,翻译远不不原版来的更好一些,下面我就贴出原版的题目,欢迎参与 ...

  4. T-SQL Recipes之Separating elements

    Separating elements Separating elements is a classic T-SQL challenge. It involves a table called Arr ...

  5. Beginners Guide To Learn Dimension Reduction Techniques

    Beginners Guide To Learn Dimension Reduction Techniques Introduction Brevity is the soul of wit This ...

  6. sql之T-SQL

    sql之T-SQL   下面就T-SQL的几个方面来分别讲解一下. 1.变量 要动态的写sql语句,就不能没有变量. 声明变量并赋值: 1 declare @i as int;--定义一个 int 类 ...

  7. Portrait Photography Beginners Guide

    Please visit photoandtips稻糠亩 for more information. 六级/考研单词: vogue, derive, gorgeous, thereby, strict ...

  8. T-SQL学习记录

    T-sql是对SQL(structure query language )的升级.可以加函数. 系统数据库:master管理数据库.model模版数据库,msdb备份等操作需要用到的数据库,tempd ...

  9. 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(下)

    索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 五.透视.逆透视及分组 5.1 透视 所谓透视( ...

随机推荐

  1. astyle代码格式化

    Artistic Style 1.24 A Free, Fast and Small Automatic Formatterfor C, C++, C#, and Java Source Code 项 ...

  2. 3.1日 重温JVM相关信息

    1.JDK.JRE.JVM的关系: JDK是java开发的必备工具箱,JDK其中有一部分是JRE,JRE是JAVA运行环境,JVM则是JRE最核心的部分. 2.JVM的组成: JVM由4大部分组成:C ...

  3. Host group 信息

  4. 14.5.5 Deadlocks in InnoDB

    14.5.5 Deadlocks in InnoDB 14.5.5.1 An InnoDB Deadlock Example 14.5.5.2 Deadlock Detection and Rollb ...

  5. 【转】android官方侧滑菜单DrawerLayout详解

    原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html drawerLayout是Support ...

  6. 【转】git在eclipse中的配置

    原文网址:http://www.cnblogs.com/zhxiaomiao/archive/2013/05/16/3081148.html 一_安装EGIT插件 http://download.ec ...

  7. Lowest Common Ancestor of a Binary Tree——Leetcode

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  8. 简单tableView的使用

    UITableView是一个用于显示列表的视图,可以作为子视图镶嵌在主视图上,可以滑动,选取各种参数 定义: @interface ViewController : UIViewController& ...

  9. (转载)绿色版Mysql的安装配置

    本文出自于:http://johnnyhg.javaeye.com/blog/245544 一.下载MySQL http://www.mysql.org/downloads 我下载的是mysql-no ...

  10. Pipe - POJ 1039(线段相交交点)

    题目大意:有一个不反光并且不透光的管道,现在有一束光线从最左端进入,问能达到的最右端是多少,输出x坐标.   分析:刚开始做是直接枚举两个点然后和管道进行相交查询,不过这样做需要考虑的太多,细节不容易 ...