employee department wise and count of employees more than 5

后端 未结 3 1041
我在风中等你
我在风中等你 2021-01-29 05:14

i want to display department_id\'s along with count,and count should be more than 5, and i want to have employees who are not hired in January.

i tried the

3条回答
  •  没有蜡笔的小新
    2021-01-29 05:57

    If you want the dept id and count of employees (where employee hire date is not in Jan) then something like the following should work. I say "something like the following" because I suspect the WHERE hire_date NOT LIKE '%JAN%' could be improved, but it would just depend on the format of that column.

       SELECT
          DEPARTMENT_ID, 
          COUNT(*) 
        FROM EMPLOYEES 
        WHERE HIRE_DATE NOT LIKE '%JAN%' 
        GROUP BY DEPARTMENT_ID 
        HAVING COUNT(*)>5;
    

    If you also want to list the individual employees along with these departments, then something like this might work:

    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%';
    

    Again, though, I think you can leverage your schema to improve the where clause on HIRE_DATE. A like/not-like clause is generally going to be pretty slow.

提交回复
热议问题