我希望显示department_id
以及计数,并且计数应该超过5,我希望员工不会在1月份被录用。
我尝试了以下查询
SELECT * FROM EMPLOYEES
WHERE DEPARTMENT_ID IN
(
SELECT DEPARTMENT_ID
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING COUNT(*)>5
)
AND HIRE_DATE NOT LIKE '%JAN%';
但在这里我没有得到数数。我也想要数数。
答案 0 :(得分:1)
如果您想要部门ID和员工数量(员工雇用日期不在1月份),那么以下内容应该有效。我说"类似于以下内容"因为我怀疑WHERE hire_date不喜欢'%JAN%'可以改进,但它只取决于该列的格式。
SELECT
DEPARTMENT_ID,
COUNT(*)
FROM EMPLOYEES
WHERE HIRE_DATE NOT LIKE '%JAN%'
GROUP BY DEPARTMENT_ID
HAVING COUNT(*)>5;
如果您还希望列出各个员工以及这些部门,那么这样的事情可能有效:
SELECT a.*, b.count(*)
FROM EMPLOYEES AS a
INNER JOIN (
SELECT
DEPARTMENT_ID,
COUNT(*)
FROM EMPLOYEES
WHERE HIRE_DATE NOT LIKE '%JAN%'
GROUP BY DEPARTMENT_ID
HAVING COUNT(*)>5) AS b
ON a.department_id = b.department_id
WHERE a.HIRE_DATE NOT LIKE '%JAN%';
但是,我认为您可以利用您的架构来改进HIRE_DATE的where子句。一个喜欢/不喜欢的条款通常会很慢。
答案 1 :(得分:1)
SELECT department_ID, count(employee_id) as '# of Employees' FROM EMPLOYEES
WHERE DEPARTMENT_ID IN
(
SELECT DEPARTMENT_ID
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING COUNT(*)>5
)
AND HIRE_DATE NOT LIKE '%JAN%'
group by department_ID;
此查询返回department_id,因为我按department_id分组,将返回属于每个部门的员工数
输出看起来像这样
Department_Id | # of Employees
1 7
2 6
4 9
答案 2 :(得分:0)
从内部查询中选择计数,然后加入:
SELECT E.*, DEPT_COUNT
FROM EMPLOYEES E
JOIN (
SELECT DEPARTMENT_ID, COUNT(*) DEPT_COUNT
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING COUNT(*) > 5
) DC ON E.DEPARTMENT_ID = DC.DEPARTMENT_ID
AND HIRE_DATE NOT LIKE '%JAN%'