在SQL Server中评估阶乘(n)?

时间:2018-09-05 14:28:35

标签: sql-server tsql

如何在SQL Server 2012中评估k!之类的数学表达式?

2 个答案:

答案 0 :(得分:1)

我真的认为在SQL Server中计算阶乘是一个坏主意,但是如果您必须这样做

with pair (n, f) as (
  select 0 as n, 1 as f
  union all
  select p.n + 1 as n, p.f * (p.n + 1) from pair p 
    where p.n < 5 -- factorial of 5
)
select f from pair where n = 5; -- factorial of 5

您需要将N的值放置两次。结果:

f
---
120

答案 1 :(得分:0)

尝试一下:

G.edges(5, data=True)
> EdgeDataView([(5, 2, {'weight': 4}), (5, 6, {'weight': 2}), (5, 3, {'weight': 14}), (5, 4, {'weight': 10})])