select t_time,avg(t_value) as t_value from tablename a where t_value in (select top 10 t_value from tablename where t_time=a.t_time order by t_value) group by t_time
解决方案 »
- 求救一个关于MSSQLSERVER的问题
- 请高人优化一下sql语句
- 前天才装的 Microsoft SQL Server 2005 CTP 里边的 LogFiles 目录竟高达 19753 个文件和 3.91G 的容量
- sql server 2005 有没有数据导入导出向导?
- 数据类型的问题
- 您没有使用大容量加载语句的权限。
- 一个简单问题,麻烦大家,怎么调用函数
- 请问怎么查看存储过程返回的结果集是正确的?
- 如何查找同类型中有重复的记录?急!在线等!!!! 我没有分了,大家救救我吧
- 求求!sql sever2000 服务器经常自动会停止。如何解决!!
- 求助SQL语句的写法
- 把表内的某几行数据更新为另几行数据,怎么写比较简单?
为了简化入数,我把datetime类型改成了varchar:create function ts ( @t varchar(2) )
returns int
as
begin
declare @i int
select @i=avg(i) from (select top 10 i from t where t=@t order by i desc) a
return @i
end
gocreate table t (t varchar(2),i int)insert t select '1',1
union select '1',2
union select '1',3
union select '1',4
union select '1',5
union select '1',6
union select '1',7
union select '1',8
union select '1',9
union select '1',12
union select '1',22
union select '1',32
union select '1',42
union select '1',52
union select '1',62
union select '1',72
union select '1',2
union select '1',82
union select '1',92
union select '1',2
union select '12',2
union select '12',22
union select '12',23
union select '12',24
union select '12',25
union select '12',26
union select '12',27
union select '12',28
union select '12',29
union select '12',20
union select '12',926
union select '12',927
union select '12',928
union select '12',929
union select '12',920select t, dbo.ts(t)
from t
group by tdrop table t
如果我要的平均值不是前10位,而是前N(0,10];我这样改怎么好象不行?
create function ts ( @t varchar(2),@n int )
returns int
as
begin
declare @i int
select @i=avg(i) from (select top @n i from t where t=@t order by i desc) a
return @i
end
top 后面不允许跟变量的。