类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
我想自己写一个集合函数(平均函数),原因是AVG不能满足我的应用要求!
请高手赐教!
网友回答:
看是不是要这个结果:
declare @t table(dt datetime,qty int)
insert @t
select 2004-01-01, 2 union all
select 2004-01-02, 3 union all
select 2004-01-05, 5 union all
select 2004-01-08, 8 union all
select 2004-01-10, 6
select avg=total)/(select datediff(day,min(dt),max(dt))+1.0 from @t) from
(
select case when exists(select * from @t a where a.dt>t.dt)
then datediff(day,dt,(select top 1 dt from @t a where a.dt>t.dt order by dt))
else 1 end*qty total
from @t t
) t