从查询中删除组,结果相同

时间:2016-02-07 06:46:03

标签: oracle group-by

任何人都可以帮助以下查询,我想修改它而不使用group by,同时我得到相同的结果。

 SELECT     EMPLOYEE_ID           ,
           FIRST_NAME            ,
           LAST_NAME             ,
           EMAIL                 ,
           PHONE_NUMBER          ,
           HIRE_DATE             ,
           JOB_ID                ,
           SALARY                ,
           NVL(COMMISSION_PCT,0) ,
           MANAGER_ID            ,
           DEPARTMENT_ID
FROM       EMPLOYEES
WHERE      EMPLOYEE_ID
IN         (SELECT MANAGER_ID
            FROM   EMPLOYEES)
OR         DEPARTMENT_ID = (SELECT DEPARTMENT_ID
                            FROM   DEPARTMENTS
                            WHERE  UPPER(DEPARTMENT_NAME) = 'SALES')
OR         TO_CHAR(HIRE_DATE,'YYYY')<=TO_CHAR(SYSDATE,'YYYY')-10
GROUP BY   EMPLOYEE_ID      ,
           FIRST_NAME       ,
           LAST_NAME        ,
           EMAIL            ,
           PHONE_NUMBER     ,
           HIRE_DATE        ,
           JOB_ID           ,
           SALARY           ,
           COMMISSION_PCT   ,
           MANAGER_ID       ,
           DEPARTMENT_ID
HAVING     AVG(SALARY) > 1000;

1 个答案:

答案 0 :(得分:0)

我找到了如下解决方案:

SELECT     E.EMPLOYEE_ID           ,
           E.FIRST_NAME            ,
           E.LAST_NAME             ,
           E.EMAIL                 ,
           E.PHONE_NUMBER          ,
           E.HIRE_DATE             ,
           E.JOB_ID                ,
           E.SALARY                ,
           NVL(E.COMMISSION_PCT,0) ,
           E.MANAGER_ID            ,
           E.DEPARTMENT_ID
FROM       EMPLOYEES E
WHERE      EMPLOYEE_ID
IN         (SELECT MANAGER_ID
            FROM   EMPLOYEES)
OR         DEPARTMENT_ID = (SELECT DEPARTMENT_ID
                            FROM   DEPARTMENTS
                            WHERE  UPPER(DEPARTMENT_NAME) = 'SALES')
OR         TO_CHAR(HIRE_DATE,'YYYY')<=TO_CHAR(SYSDATE,'YYYY')-10
OR         SALARY > (SELECT AVG(SALARY) FROM EMPLOYEES E2 WHERE E2.DEPARTMENT_ID = E.DEPARTMENT_ID);