----------[ 求 一 sql 语句 ]++++++++++++求名称中各个成员在总表中出现的百分比.表:NAME    L其他字段
A        xxx
A        xxx
B        xxx
B        xxx
B        xxx希望结果:name p
A    40%
B    60%

解决方案 »

  1.   

    select name, count(1)*100.0/nullif((select count(1) from tab),0) from tab group by name
      

  2.   


    declare @t table (NAME varchar(1),L其他字段 varchar(3))
    insert into @t
    select 'A','xxx' union all
    select 'A','xxx' union all
    select 'B','xxx' union all
    select 'B','xxx' union all
    select 'B','xxx'select NAME,
    百分比=ltrim(cast(cast(count(*) as decimal(16,2))*100/
    (select count(*) from @t) as decimal(18,0)))+'%'
    from @t group by NAME
    /*
    NAME 百分比
    ---- ------------------------------------------
    A    40%
    B    60%
    */
      

  3.   

    select name,rtrim(count(1)*100./(select count(1) from tb))+'%'
    from tb group by name
      

  4.   


    create table #t (aa varchar(10))
    insert into #t
    select 'A' union all
    select 'A' union all
    select 'B'union all
    select 'B'union all
    select 'B'
    select aa,t,cast(t as decimal(8,2))/t2 *100 from 
    (select aa,count(*) as t  from #t group by aa) a left join
    (select count(*) as t2  from #t ) b on 1=1
      

  5.   

    select
     name,rtrim(count(1)*100./(select count(1) from tb))+'%' as 百分比
    from
     tb 
    group by
     name