有没有办法获得每个字段的百分比

Is there a way to get percentage for earch field

我需要每个状态的百分比

Select NAME, STATUS, Count(*) From DATA 

group by NAME, STATUS
order by NAME

我认为您在百分比计算中使用了以下公式, count(row first count)*100/所有count行的总和,即448*100/1560=28.71。 所以,亲爱的,我请求你在你的问题中提到所有问题。

尝试下面的查询并享受它。

 Select NAME, STATUS, Count(*),
     Count(*)*100/(select sum(CT) from (select NAME, STATUS, Count(*) CT from data group by NAME, STATUS))PERCENTAGE
     From DATA 
       group by NAME, STATUS
        order by NAME

您需要为没有状态的计数创建子查询,然后您需要明智地获取百分比,您可以在下面尝试:

Select f1.NAME, f1.STATUS, Count(*),CONCAT((Count(*)*100)/(select Count(*) from data f2 where NAME=f1.Name group by f2.NAME),'%') From DATA f1

group by f1.NAME, f1.STATUS
order by f1.NAME

或者如果你有用户id那么它是更好的查询和完美的结果

Select f1.NAME, f1.STATUS, Count(*),CONCAT((Count(*)*100)/(select Count(*) from data f2 where UserId=f1.UserId group by f2.UserId),'%') From DATA f1

group by f1.UserId,f1.NAME, f1.STATUS
order by f1.NAME

这是获取百分比的方法。首先是得到总数 count(),然后乘以 1.0,将结果转换为 decimal 格式。

select name, status, count(1) 
    ,  ((count(1) * 1.0)
        /(select count(1) from data)) * 100 as percentage
from data
group by name, status
order by name

检查这个,使用 SUM 计算总数,然后使用 group by 对名称进行分组

SELECT name, status, COUNT(status)/(
    SELECT COUNT(status) 
    FROM data
)*100
FROM percentage
GROUP BY name, status
ORDER BY name