我是MYSQL的新手,我有下表。目标是返回具有最高百分比的工程员工的deptID。
empID empName job deptID salary
68 Morris secretary 3 23000
69 Maria engineer 3 32000
45 Kelly engineer 7 37000
77 Hergot engineer 7 28000
66 Hess technician 7 32000
92 Mays engineer 7 45000
89 Williams engineer 12 36000
23 Smith programmer 13 35000
56 Herr janitor 13 26000
以下是我的代码不起作用:
SELECT deptID
FROM Employee
WHERE job = 'engineer'
GROUP BY deptID
order by (SELECT COUNT(*) FROM employee WHERE job='engineer')/count(*) DESC
所需的输出是:
deptID
12
因为对于deptID 12,它只有一名员工中的一名工程师,其工程师比例最高。
提前感谢您的帮助。
我认为这是解决方案。谢谢你回答。
SELECT
deptID, SUM(job = 'engineer') / COUNT(*) AS perc
FROM
Employee
GROUP BY deptID
ORDER BY perc DESC
LIMIT 1;
答案 0 :(得分:0)
这是一个难题......
SELECT deptid
, SUM(job = '?')/?????(*) * 100 pct
FROM my_table
GROUP BY ??;
假设没有关系,那么我们就已经完成了,否则还有另一个步骤