declare @t table(年龄 int)
insert into @t select 22
union all select 23
union all select 24
union all select 22
union all select 25select 
  年龄,
  count(年龄) as 频数,
  rtrim(cast((count(年龄)+0.0)/(select count(*) from @t) as float)*100)+'%' as 百分比
from 
  @t
group by 
  年龄
order by
  年龄

解决方案 »

  1.   

    create table #t(id int)go
    insert #t
    select 22
    union all select 23
    union all select 24
    union all select 22
    union all select 25
    goselect id,count(*) 频数,cast(count(*) as decimal(8,2))/(select count(*) from #t) 百分比
    from #t
    group by id
      

  2.   

    select 年龄, count(年龄), convert(float, count(年龄) / convert(float, (select count(t1.年龄) from 表 t1))) from 表 group by 年龄不用弄成%百分比的样子了.将来不好取.就让它是小数即可.将来取出来转成百分数就很方便了.
      

  3.   

    declare @t table(年龄 int)
    insert into @t select 22
    union all select 23
    union all select 24
    union all select 22
    union all select 25select 
      年龄,
      count(年龄) as 频数,
      cast((count(年龄)+0.0)/(select count(*) from @t) as dec(10,1)) as 百分比
    into #t
    from 
      @t
    group by 
      年龄
    order by
      年龄select 
      a.年龄,
      a.频数,
      (select sum(isnull(频数,0)) from #t where 年龄<=a.年龄) as 累计频数,
      rtrim(百分比*100)+'%' as 百分比,
      rtrim((select sum(isnull(百分比,0)*100) from #t where 年龄<=a.年龄))+'%' as 累计百分比
    from #t adrop table #t--这样?
      

  4.   

    create table taa(年龄 int)
    insert into taa select   22
    union all select   23
    union all select    24
    union all select    22
    union all select    25select 年龄,RTRIM(cast(cast(count(*)*100.0/(select count(*) from taa) as dec(6,2))as char(10)))+'%',
    RTRIM(cast(cast((select count(*) from taa where 年龄<=a.年龄)*100.0/(select count(*) from taa) as dec(6,2))as char(10)))+'%'
    from taa a group by 年龄
      

  5.   

    select 年龄,count(*)频数,
    (select count(*) from taa where 年龄<=a.年龄)累计频数,
    RTRIM(cast(cast(count(*)*100.0/(select count(*) from taa) as dec(6,2))as char(10)))+'%' 百分比,
    RTRIM(cast(cast((select count(*) from taa where 年龄<=a.年龄)*100.0/(select count(*) from taa) as dec(6,2))as char(10)))+'%' 累计百分比
    from taa a group by 年龄