欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sql的AVG如何实现保留一位小数(数据库里为int型)?

程序员文章站 2022-07-12 22:15:33
...
   
   select b.unitCode,b.unit,AVG(b.age*1.0)    avgAge from  (select ((2015-SUBSTRING(birthday,0,5))*12+(3-SUBSTRING(birthday,5,2)))/12 as age
,a.* from TRT_CADRES a) b where unit is not null group by b.unitCode,b.unit


AVG(b.age*1.0) 

select case when avg(colname*1.0)=cast(avg(colname*1.0) as int) then avg(colname*1.0) else cast(avg(colname*1.0) as decimal(9,1)) end