我有一个数据表员工,我想要显示在雇用最多雇员人数的同一天雇用的所有员工。这是我的代码:
select last_name, hire_date, count(*) as "Number of Employees Hired"
from employees
group by last_name, hire_date
where hire_date = max(count(*));
但是,此代码显示" SQL命令未正确结束"我运行它时出错。如何修复它以使其正确运行?
答案 0 :(得分:1)
这可能不是最干净的方式,但我认为会做到这一点:
SELECT last_name,
max_hire.hire_date,
max_hire.cnt
FROM
(SELECT *
FROM
(SELECT hire_date,
COUNT(*) cnt
FROM employees
GROUP BY hire_date
ORDER BY COUNT(*) DESC
)
WHERE rownum = 1
) max_hire
INNER JOIN employees
ON employees.hire_date = max_hire.hire_date
答案 1 :(得分:0)
首先,您应该在WHERE
子句之前使用GROUP BY
,其次应该正确使用聚合函数(count
),因为在您的情况下count(*)
无法进行比较在WHERE
条款中。
您可以使用以下方法而不使用where:(在MySQL下测试)
select last_name, hire_date, count(*) as "Number of Employees Hired"
from employees
group by last_name, hire_date
Order by "Number of Employees Hired" desc limit 1
希望这会有所帮助。欢呼声。