Calculation of percentage of group count(*)

前端 未结 4 748
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 02:58
Select * from Namelist;
Name      Age
Sathish   25
Sathish   65
Sathish   55
Sathish   45
Sathish   35
Jana      55
Jana      25
Jana      10
Bala      55
Bala               


        
4条回答
  •  感动是毒
    2021-01-01 03:23

    This is a slightly sexier version of some of the other answers - note the use of sum(100) to avoid the longer (and more mundane) count(*) * 100 :)

    select name, count(*) as count, sum(100) / total as percentage
    from namelist
    cross join (select count(*) as total from namelist) x
    group by 1
    

提交回复
热议问题