在编写此查询时,如何摆脱这个额外的grouping_id列?

时间:2014-05-08 01:59:28

标签: sql oracle sqlplus

所以我编写了这个查询,但是我需要摆脱grouping_id列。我试着把它从查询语句中删除但由于最后的having子句有错误需要grouping_id ...怎么做我解决了这个???

执行后,输出应该看起来像这样,减去group_id列:

SQL> SELECT DNAME, GENDER, GROUPING_ID(DNAME,GENDER) AS "Group_id", SUM(SALARY) FROM AZDEPARTMENT JOIN
 AZCONSULTANT ON AZDEPARTMENT.DID=AZCONSULTANT.DID GROUP BY CUBE(DNAME,GENDER) HAVING GROUPING_ID(DN
AME,GENDER)>0;

DNAME                                    G     Group_ID SUM(SALARY)
---------------------------------------- - ---------- -----------
                                                    3     1059000
                                         F          2      366500
                                         M          2      692500
Risk and Compliance                                 1      264500
Finance and Accounting Excellence                   1      395000
Internal Audit and Financial Controls               1      399500

6 rows selected.

1 个答案:

答案 0 :(得分:1)

您可以使用子查询执行此操作:

SELECT DNAME, GENDER, SUMSALARY
FROM (SELECT DNAME, GENDER, GROUPING_ID(DNAME,GENDER) AS "Group_id", SUM(SALARY) AS SUMSALARY
      FROM AZDEPARTMENT JOIN
           AZCONSULTANT
           ON AZDEPARTMENT.DID=AZCONSULTANT.DID
      GROUP BY CUBE(DNAME,GENDER)
      HAVING GROUPING_ID(DNAME,GENDER) > 0
     ) T