计算组数百分比(*) [英] Calculation of percentage of group count(*)

查看:130
本文介绍了计算组数百分比(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Select * from Namelist;
Name      Age
Sathish   25
Sathish   65
Sathish   55
Sathish   45
Sathish   35
Jana      55
Jana      25
Jana      10
Bala      55
Bala      26

如何获取给定格式的百分比值;

How to get Percentage value for given format;

Name   Count   Percentege
Sathish  5     50%
Jana     3     30%
Bala     2     20%

好心地共享sql查询吗?

Kindly share sql query?

推荐答案

这是其他一些答案的略带性感的版本-请注意使用sum(100)来避免更长(或更平凡)的count(*) * 100: )

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

这篇关于计算组数百分比(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆