This is the code
Create Function dbo.HybFillRate(@percen Float=0.9, @strTable varchar(max), @strField Varchar(max), @strCriteria As Varchar(Max)=Null)
RETURNS Float
AS
BEGIN
--Drop table HyFilrate
--Select * from Hyfilrate
--Create table HyFilrate(Soct Int, Countup Int)
Declare @HyFilrate table (Soct Int, Countup Int)
Delete from @HyFilrate
Declare @strsql varchar(max)
Declare @strTable Varchar(max)
Declare @strField Varchar(max)
Declare @strCriteria Varchar(max)
Declare @Key float
Declare @KeyPrev float
Declare @Item float
Declare @ItemPrev float
Declare @percen float
Declare @a float
Declare @i float
Declare @counter float
Declare @Fillrate float
Declare @b float
set @strTable = 'EDW_SC'
set @strField = 'sOCT'
set @strCriteria='Soct<>0'
Set @percen='0.9'
set @strsql = 'select ' + @strField + ' as sOCT ,count(' + @strField + ') as countup into @HyFilrate from ' + @strTable
set @strsql = @strSQL + ' where (' + @strField + ') Is Not Null'
if (@strCriteria <> '')
set @strsql = (@strSQL + ' AND ' + @strCriteria)
Else
set @strsql = @strsql
set @strsql = @strsql + ' group by ' + @strField + ' order by ' + @strField
Exec (@strsql)
--Select * from HyFilrate
set @a=(SELECT Count(Countup) FROM @HyFilrate)
set @i=1
Set @counter=0
WHILE @i<= @a
BEGIN
set @b= (select Countup from(select sOCT,Countup,row_number() over(order by sOCT) as 'row' from @HyFilrate)as temp where row=@i)
Set @Counter=@b+@counter
Set @i=@i+1
End
set @KeyPrev = 0
set @i=1
set @Item=0
WHILE @i<= @a
BEGIN
set @ItemPrev = @Item
Set @Item = (select Countup from(select sOCT,Countup,row_number() over(order by sOCT) as 'row' from @HyFilrate)as temp where row=@i)+ (select Countup from(select sOCT,Countup,row_number() over(order by sOCT) as 'row' from HyFilrate)as temp where row=@i-1)
Set @key=(select sOCT from(select sOCT,Countup,row_number() over(order by sOCT) as 'row' from @HyFilrate)as temp where row=@i)--key
Set @Keyprev=(select sOCT from(select sOCT,Countup,row_number() over(order by sOCT) as 'row' from @HyFilrate)as temp where row=@i-1)--keyPrev
if (@Item/@counter>=@Percen)
Set @Fillrate = (@KeyPrev + ((@Key - @KeyPrev) * ((@percen - (@ItemPrev / @counter)) / ((@Item - @ItemPrev) / @counter))))
END
Return @Fillrate
END
我无法在函数中使用临时表,而且我不知道如何使用函数内部的select查询结果找到matrics,基于那个selct查询函数我只能计算.... ....
我是sql server的新手,有点帮助我plsssssss ........ 提前致谢