多项式展开系数

Declare @A int=7;
Declare @w nVarchar(3000)=''
Declare @s nVarchar(3000)=''
Declare @sql nvarchar(4000)=''

Select  
	@w=@w+',['+number+']',
	@s=@s+',isnull(['+number+'],'''') As ['+number+']'
	From
	(
		Select convert(Varchar(10),number) As number
		From master.dbo.spt_values
			Where type='P'  and number between 1 and 2*(@A+1)
	) S

Set @sql=
'
With T
As
(
	Select  number From master.dbo.spt_values
		Where type=''P'' And number between 1 and @A+1
)
Select '+Stuff(@s,1,1,'')+' From
(
	Select A.number*[email protected] As A,B.number As B,
		Convert(Varchar(10),nullif(dbo.fn_c(A.number-1,b.number),0)) As C
		From  T A cross join T B
			Where b.number<=@A
) s
Pivot
(
	max(c)
	for
	A in('+Stuff(@w,1,1,'') +')
)p'

exec sp_executesql @sql,N'@A int',@A



--排列
Create Function fn_p(@I int)
Returns int
As
Begin
	Declare @Rst int=1
	While @I>0
	Begin
		Set @Rst=@Rst*@I
		Set @I=@I-1
	End
	Return @Rst
end

--组合
Create Function fn_c(@n int,@m int)
Returns int
As
Begin
	 return dbo.fn_p(@m)/(dbo.fn_p(@n)*dbo.fn_p(@m-@N))
End

多项式展开系数_第1张图片

你可能感兴趣的:(SQL,Server)