Question

So I wrote this query but I need to get rid of the grouping_id column..I tried just leaving it out of the query statement but its having errors due to the having clause at the end which requires grouping_id...How do I fix this???

Output should look something like this after executed minus the group_id column:

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.
Was it helpful?

Solution

You could do this using a subquery:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top