declare @t table(type varchar(3))
insert @t select 'aa'
insert @t select 'aa'
insert @t select 'bb'select type ,count(type)/(select count(1) from @t) as cnt
from @t
group by type

解决方案 »

  1.   

    不是写过了?create table tb(id varchar(10))
    insert into tb values('a')     
    insert into tb values('a') 
    insert into tb values('a') 
    insert into tb values('b') 
    insert into tb values('b') 
    insert into tb values('b') 
    insert into tb values('c') 
    insert into tb values('c') 
    insert into tb values('c') 
    insert into tb values('d') 
    insert into tb values('d') 
    insert into tb values('d') 
    insert into tb values('a') 
    insert into tb values('a') 
    insert into tb values('a') 
    goselect id , 数量 = count(*) , [百分比(%)] = cast(count(*)*100.0/(select count(*) from tb) as decimal(18,2)) from tb group by iddrop table tb/*
    id         数量          百分比(%)
    ---------- ----------- ---------------------------------------
    a          6           40.00
    b          3           20.00
    c          3           20.00
    d          3           20.00(4 行受影响)
    */
      

  2.   

    select type ,count(type)*100.00/(select count(1) from @t) as cnt 
    from @t 
    group by type
      

  3.   

    create table tb(id varchar(10))
    insert into tb values('aa')     
    insert into tb values('aa') 
    insert into tb values('aa') 
    insert into tb values('bb') 
    insert into tb values('bb') 
    insert into tb values('cc') 
    insert into tb values('cc') 
    insert into tb values('dd') 
    insert into tb values('dd') 
    goselect id , 数量 = count(*) , [百分比(%)] = cast(count(*)*100.0/(select count(*) from tb) as decimal(18,2)) from tb group by iddrop table tb/*
    id         数量          百分比(%)
    ---------- ----------- ---------------------------------------
    aa         3           33.33
    bb         2           22.22
    cc         2           22.22
    dd         2           22.22(4 行受影响)
    */
      

  4.   

    declare @t_table(type varchar(3))
    select type ,count(type)/(select count(1) from @t_table) as cnt from @t_table group by type