有表T1
T1里有字段
typy(varchar),rate(float),fee(float)三个字段
我想统计每一行的(fee/rate,并四舍五入取整),并按typy类型分组,统计分组的fee/rate值的总和。

解决方案 »

  1.   


    select typy,sum(CAST(fee/rate as numeric(12,2))) as groupValue
    from tb
    group by typy
    union all
    select '总值',sum(CAST(fee/rate as numeric(12,2)))
    from tb
      

  2.   


    select typy,sum(case when rate<>0 then round(fee/rate,2) else 0 end) as total
    from t1 group by typy with rollup
      

  3.   


    select typy, sum(round(fee/rate,0)) 总和
    from T1
    group by typy
    这个是算的整数。简单些,但前提是rate不能为0或者空
      

  4.   

    select typy, sum(round(case rate when 0 then 0 else fee/rate end,0)) 总和
    from T1
    group by typy