怎么控制avg函数的返回值类型啊?
字段里的数据都是int型的,但是我avg之后想要的数据类型是带小数的
怎么实现?
比如对(3,4)求avg,得到的应该是3.5
我需要对3.5进行四舍五入,即:得到4
这个要怎么实现啊?

解决方案 »

  1.   


    select cast( round(3.5,0) as int),cast( round(3.46,0) as int)
                           
    ----------- -----------
              4           3(1 行受影响)
      

  2.   

    进1==》
    ceiling()
    舍去小数==>
    floor()
      

  3.   

    select cast(round(3.5,0) as int)-----------
    4(1 行受影响)
      

  4.   

    select round(sum(col)*1.0/(select count(*) from(select 3 col union select 4)t),0)
    from
    (select 3 col
     union 
     select 4)t
    /*
    ---------------------------------------
    4.000000000000(1 個資料列受到影響)*/
      

  5.   

    ceiling
    round
    floor
    等函数
      

  6.   

    round 函数
    cast
    convert 等进行强转
      

  7.   

    declare @t table
    (
     i int
    )
    insert into @t select 3
    insert into @t select 4
    select round(avg(i*1.0),0) from @t---------------------------------------
    4.000000(1 行受影响)