资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

高分求助,问SQLServer中的AVG函数的工作原理?

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

我想自己写一个集合函数(平均函数),原因是AVG不能满足我的应用要求!  
  请高手赐教!

网友回答:

发表者:pbsql

看是不是要这个结果:  
  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


 

 
热门推荐笔记本: 联想笔记本
相关文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved