SQL中的Percentile算法怎么实现

zuiaimilan 2009-04-02 08:55:35
怎么在SQL server中实现和Percentile算法一样的功能呢 哪位好心人给写一个存储过程吧!很急,不胜感激!Percentile(Field_name)其中参数值Field_name是数据库中一个表的列名,就是对Field_name这一列进行Percentile取的K值。
...全文
1173 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2009-04-02
  • 打赏
  • 举报
回复
参考下Z老大的

   
create function f_calc(
@K decimal(3,2),
@Class int
)returns decimal(10,2)
as
begin
declare @re decimal(10,2)
if @K between 0 and 1
begin
declare @array table(id int identity(1,1),value int)
insert @array select Salary
from 表
where Class=@Class order by Salary
set @re=(1-((@@rowcount-1)*@K-ROUND((@@rowcount-1)*@K,0,-1)))
*(select value from @array where id=ROUND((@@rowcount-1)*@K,0,-1)+1)
+((@@rowcount-1)*@K-ROUND((@@rowcount-1)*@K,0,-1))
*(SELECT value from @array where id=ROUND((@@rowcount-1)*@K,0,-1)+2)
end
return(@re)
end
go

--测试

--测试数据
create table 表(Class int,Salary int,Median decimal(10,2))
insert 表 select 1,1000,NULL
union all select 1,2000,NULL
union all select 1,3000,NULL
union all select 1,4000,NULL
union all select 2,5000,NULL
union all select 2,6000,NULL
union all select 2,7000,NULL
union all select 2,8000,NULL
union all select 2,9000,NULL
union all select 2,10000,NULL
go

--调用函数进行更新
update 表 set Median=dbo.f_calc(0.3,Class)
go

--显示更新结果
select * from 表
go

--删除测试
drop table 表
drop function f_calc

/*--测试结果

Class Salary Median
----------- ----------- ------------
1 1000 1900.00
1 2000 1900.00
1 3000 1900.00
1 4000 1900.00
2 5000 6500.00
2 6000 6500.00
2 7000 6500.00
2 8000 6500.00
2 9000 6500.00
2 10000 6500.00

(所影响的行数为 10 行)
--*/
claro 2009-04-02
  • 打赏
  • 举报
回复
帮顶。
zuiaimilan 2009-04-02
  • 打赏
  • 举报
回复
我这个程序里K=0.8是固定的
zuiaimilan 2009-04-02
  • 打赏
  • 举报
回复
哦 对了 少写了一个K值
  • 打赏
  • 举报
回复
看看

--借用楼上的计算公式来写计算函数

create function f_calc(
@K decimal(3,2),
@Class int
)returns decimal(10,2)
as
begin
declare @re decimal(10,2)
if @K between 0 and 1
begin
declare @array table(id int identity(1,1),value int)
insert @array select Salary
from 表
where Class=@Class order by Salary
set @re=(1-((@@rowcount-1)*@K-ROUND((@@rowcount-1)*@K,0,-1)))
*(select value from @array where id=ROUND((@@rowcount-1)*@K,0,-1)+1)
+((@@rowcount-1)*@K-ROUND((@@rowcount-1)*@K,0,-1))
*(SELECT value from @array where id=ROUND((@@rowcount-1)*@K,0,-1)+2)
end
return(@re)
end
go

--测试

--测试数据
create table 表(Class int,Salary int,Median decimal(10,2))
insert 表 select 1,1000,NULL
union all select 1,2000,NULL
union all select 1,3000,NULL
union all select 1,4000,NULL
union all select 2,5000,NULL
union all select 2,6000,NULL
union all select 2,7000,NULL
union all select 2,8000,NULL
union all select 2,9000,NULL
union all select 2,10000,NULL
go

--调用函数进行更新
update 表 set Median=dbo.f_calc(0.3,Class)
go

--显示更新结果
select * from 表
go

--删除测试
drop table 表
drop function f_calc

/*--测试结果

Class Salary Median
----------- ----------- ------------
1 1000 1900.00
1 2000 1900.00
1 3000 1900.00
1 4000 1900.00
2 5000 6500.00
2 6000 6500.00
2 7000 6500.00
2 8000 6500.00
2 9000 6500.00
2 10000 6500.00

(所影响的行数为 10 行)
--*/

不过楼主的好像少一个参数
zuiaimilan 2009-04-02
  • 打赏
  • 举报
回复
就是想在sql server2000上写一个存储过程实现和excel中Percentile函数一样的功能 请高手帮帮忙呀!
playwarcraft 2009-04-02
  • 打赏
  • 举报
回复
Percentile 百分比??? select top 30 percent * from T order by field_name 嗎??
不懂。。。。
sdhdy 2009-04-02
  • 打赏
  • 举报
回复
友情帮顶!
Teng_s2000 2009-04-02
  • 打赏
  • 举报
回复
UP,不懂!!
lzfrab 2009-04-02
  • 打赏
  • 举报
回复
帮顶..

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧