编写计算n!(n=20)的SQL语句,并显示计算结果

╮(╯_╰)╭

语句为:  

with temp1(col1) as

(select 1 col1

union all

select col1+1 col1

from

temp1

where col1+1<=20

)

select exp(sum(log(col1)))

from temp1;

扩展资料:

注意事项

sql中没有乘法函数,所以一般用:

logx+logy=logx*y

实现方式,先对记录取对数log(),然后sum聚合,最后exp,结果就是记录相乘的结果。

SUM函数的语法是:

SELECT SUM(expression )

FROM tables

WHERE predicates;

表达式可以是一个数值字段或公式。

例如,可能想知道合并全体员工的薪金总额美元以上,其薪酬是25,000/年

SELECT SUM(salary) as "Total Salary"

FROM employees

WHERE salary > 25000;

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-09-24

循环求乘法?

 

要求加分,,,

sqlserver2005及以上版本可用
 
with temp1(col1) as
(select 1 col1
union all
select col1+1 col1
from
temp1
where col1+1<=20
)
select exp(sum(log(col1)))
from temp1;

本回答被网友采纳
第2个回答  推荐于2018-03-21
Declare @a decimal(28, 0)
Set @a = 1
Select @a = @a * RecID
From (
Select Top 20 name, ROW_NUMBER() Over (Order By name) As RecID
From syscolumns a) b

Select @a
--2432902008176640000本回答被提问者采纳
第3个回答  2018-07-10
declare @n int,@sum bigint
select @n=1,@sum=1
while @n<=20
begin
select @sum=@n*@sum
select @n=@n+1
end
select @sum
go
n!=2432902008176640000
相似回答