select 
    id,
    F1,
    F2,
    rtrim(cast((select count(F2) from 表)*100.0/(select sum(F2) from 表) as numeric(5,2)))+'%' 
from 
    表

解决方案 »

  1.   

    F2字段要分组统计,然后在除以F2字段总和,所select count(F2) from 表,这样不行呀.
      

  2.   

    select 
        id,
        F1,
        F2,
        cast(cast((select count(F2) from #1 where F2=a.F2)*100.0/(select count(F2) from #1) as numeric(5,2)) as varchar)+'%'
    from 
       #1 a
      

  3.   

    select count(F2) from #1 where F2=a.F2
    这样的结果都成了一样了
      

  4.   

    Create Table #1(id int,F1 varchar(10),F2 varchar(10))insert into #1
    select
    1  ,   'A'  ,  'A'
    union all select 
    2   ,  'B'  ,  'BB'
    union all select 
    3   ,  'C'  ,  'CC'
    union all select 
    4   ,  'D' ,   'DD'
    union all select 
    5  ,   'E' ,   'EE'
    union all select 
    6  ,   'F'  ,  'FF'
    union all select 
    7  ,   'G' ,   'GG'
    union all select 
    8  ,  'H' ,  'FF'
    union all select 
    9   ,  'I'  ,  'GG'
    select 
        id,
        F1,
        F2,
        cast(cast((select count(F2) from #1 where F2=a.F2)*100.0/(select count(F2) from #1) as numeric(5,2)) as varchar)+'%'
    from 
       #1 a结果如下所示:
    1 A A 11.11%
    2 B BB 11.11%
    3 C CC 11.11%
    4 D DD 11.11%
    5 E EE 11.11%
    6 F FF 22.22%
    7 G GG 22.22%
    8 H FF 22.22%
    9 I GG 22.22%