如何筛选最前面2条记录的平均数avg
1.0 2.0 3.0
4.0 5.0 3.0
7.0 2.0 5.0
5.0 5.0 5.0
不加其他任何索引,不改变表结构

解决方案 »

  1.   


    select avg(colname) from 
    (
    select top 2 colname from tb
    ) t
      

  2.   

    [code=SQL]select [col1],
           [col2],
           [col3],
           [col]=(select avg([col1])
                  from (select [col1] 
                      union all select [col2]
                      union all select [col3])T)
    from tb
    [/code]
      

  3.   

    select [col1],
           [col2],
           [col3],
           [col]=(select avg([col1])
                  from (select [col1] 
                      union all select [col2]
                      union all select [col3])T)
    from tb
      

  4.   

    select avg(col1),avg(col2),avg(col3) from
    (
      select top 2 * from tb
    ) t
      

  5.   


    select *,id=identity(int,1,1) into #t from tbselect avg(colname) from
    (select top 2 colname from #t order by id desc
    )t
      

  6.   

     order by id desc这里用了倒叙
    这里只需要对一个进行倒叙还算好,问题如果是我的数据库却还要两个倒叙
    在这两列数据都是正序的情况下,几百W条数据居然用到了差不多分钟级别的时间,
    而我取的只是几百条数据而已,所以....
      

  7.   

    呵呵,不是吧~你再看看我的需求好吗??
    select top 400 日期+' '+时间 as 年月日时分秒--
    from tableA
    order by 日期 desc,时间 desc数据量:773221[按日期和时间正序]
    SQL用的时间:3分41秒,只执行一次哦,呵呵,这让我怎么接受呀