TSQL Beginners Challenge 3 - Find the Factorial
这是一个关于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的更多相关文章
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- TSQL Challenge 2
和之前发布的TSQL Challenge 1是同一系列的文章,看到那篇学习哪篇,没有固定的顺序,只为锻炼下思维. Compare rows in the same table and group th ...
- TSQL Challenge 1
在老外网站发布的一些SQL问题,拿过来自己搞一下,后面我也会陆续转载一些问题,欢迎看到的朋友贴出自己的答案,交流一哈.对于技术问答题的描述,翻译远不不原版来的更好一些,下面我就贴出原版的题目,欢迎参与 ...
- T-SQL Recipes之Separating elements
Separating elements Separating elements is a classic T-SQL challenge. It involves a table called Arr ...
- Beginners Guide To Learn Dimension Reduction Techniques
Beginners Guide To Learn Dimension Reduction Techniques Introduction Brevity is the soul of wit This ...
- sql之T-SQL
sql之T-SQL 下面就T-SQL的几个方面来分别讲解一下. 1.变量 要动态的写sql语句,就不能没有变量. 声明变量并赋值: 1 declare @i as int;--定义一个 int 类 ...
- Portrait Photography Beginners Guide
Please visit photoandtips稻糠亩 for more information. 六级/考研单词: vogue, derive, gorgeous, thereby, strict ...
- T-SQL学习记录
T-sql是对SQL(structure query language )的升级.可以加函数. 系统数据库:master管理数据库.model模版数据库,msdb备份等操作需要用到的数据库,tempd ...
- 《MSSQL2008技术内幕:T-SQL语言基础》读书笔记(下)
索引: 一.SQL Server的体系结构 二.查询 三.表表达式 四.集合运算 五.透视.逆透视及分组 六.数据修改 七.事务和并发 八.可编程对象 五.透视.逆透视及分组 5.1 透视 所谓透视( ...
随机推荐
- Hibernate 缓存 关于注解方式
要引入 import org.hibernate.annotations.Cache; 在类前面添加: @Cache(usage= CacheConcurrencyStrategy.NONSTRICT ...
- LeetCode_3 sum closet
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 重载VerifyRenderingInServerForm
重载VerifyRenderingInServerForm 今天在做一个AjaxPro的应用时,VerifyRenderingInServerForm给我带来了麻烦,在网上找了找,发现大多数人的解决方 ...
- Batik - 将svg转换成其他格式图片或PDF - [导出服务器配置] 导出服务器原理解析
导出服务器原理解析 Highcharts图表导出(或下载)本质上是将SVG代码转换为不同文件格式的过程,用到的工具是batik,所以所谓导出服务器,只不过是调用batik,将SVG代码转换并下载.下图 ...
- Apache Hadoop RPC Authentication 安全绕过漏洞
漏洞名称: Apache Hadoop RPC Authentication 安全绕过漏洞 CNNVD编号: CNNVD-201308-425 发布时间: 2013-08-28 更新时间: 2013- ...
- Linux Kernel 'perf_event.c'本地权限提升漏洞
漏洞版本: Linux Kernel 3.11-rc4 漏洞描述: Linux Kernel是一款开源的操作系统 Linux Kernel 'perf_event.c'存在一个安全漏洞,允许本地攻击者 ...
- css3倒影
使用CSS3制作倒影 img { -webkit-box-reflect: below 0px -webkit-gradient(linear, left top, left bottom, from ...
- Linux学习笔记10——文件I/O之一
UNIX系统中的大多数文件I/O只需要用到5个函数:open,read,write,lseek以及close 文件描述符 文件描述符是一个非负整数,所有打开的文件都通过文件描述符引用 文件描述符的变化 ...
- 怎么把GPUImageFIlter处理过的图像保存成UIImage
总共有两种方法能够把GPUImage处理过的图片转化成UIImage 方法一: UIImage *inputImage = [UIImage imageNamed:@"Lambeau ...
- 国内maven镜像
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...