我对SQL比较陌生。每个员工访问一个帐户进行技术测试,有时这是一个很好的尝试,有时候很糟糕,所以我需要计算大部分不良尝试的百分比,我的报告看起来应该是这样的:
选择 员工,事件,总数,百分比 来自my_table
employee | event | total | percentage|
user1 | good | 50 | 50% |
user1 | bad | 50 | 50% |
答案 0 :(得分:0)
尝试这样的计算每位员工的bad
事件百分比
select employee,(sum(case when event = 'bad' then 1 else 0 end) / count(*)) * 100
From Yourtable
Group by employee
答案 1 :(得分:0)
计算子查询中的总数,然后JOIN
计算每行的百分比。
SELECT employee, event, COUNT(*), COUNT(*) * 100.0 / t.total as percentage
FROM my_table
JOIN (SELECT employee, count(*) total
FROM my_table
GROUP BY employee) T
ON my_table.employee = t.employee
GROUP BY employee, event