求个sql语句ID       类别
1 A
2 A
3 A
4 B
5 B
6 B
7 C
8 A
9 D
10 C
如何知道各个类别占总数的百分比

解决方案 »

  1.   

    select 类别,count(1)*1.0/count(1)over() from tb group by 类别
      

  2.   

    select [类别],count(1)/(select count(1) from [表])
    from [表]
    group by [类别]
      

  3.   

    select
     [类别],ltrim(count(1)/(select count(1) from [表]))+'%' as 百分比
    from
     [表]
    group by
     [类别]
      

  4.   


    这个结果好像有问题 我想要的结果是A 40% B30% C20% D10%
      

  5.   


    select
     [类别],ltrim(cast(count(1)*100./(select count(1) from [表]) as decimal(6,2)))+'%' as 百分比
    from
     [表]
    group by
     [类别]
      

  6.   


    select 类别,ltrim(cast(count(1)*100.0/count(1)over() as decimal(6,2))) + '%' as [百分比]
    from tb 
    group by 类别
      

  7.   

    select
     [类别],ltrim(cast(count(1)*100.0/(select count(1) from [表]) as decimal(18,2)))+'%' as 百分比
    from
     [表]
    group by
     [类别]