Skip to Main Content
  • Questions
  • Print sub-total and total name in SQL

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sagar.

Asked: February 14, 2018 - 1:53 pm UTC

Last updated: February 14, 2018 - 4:15 pm UTC

Version: 12.2.0.1.0

Viewed 1000+ times

You Asked

Hi Team

Code sample provided in LiveSQL link.

Expected Result:
Mgmt1 State1    18900 1786
Mgmt1 State2    28900 2786
Mgmt1   SUB_TOTAL  47800 4572
Mgmt2 State1    38900 3786
Mgmt2 State2    48900 4786
Mgmt2 SUB-TOTAL  87800 8572
Mgmt3 State1    58900 5786
Mgmt3 SUB_TOTAL  58900 5786
TOTAL   (null)    194500 18930


Result with current SQL:
Mgmt1 State1    18900 1786
Mgmt1 State2    28900 2786
Mgmt1   (null)     47800 4572
Mgmt2 State1    38900 3786
Mgmt2 State2    48900 4786
Mgmt2 (null)     87800 8572
Mgmt3 State1    58900 5786
Mgmt3   (null)     58900 5786
(null)  (null)    194500 18930


Thanks...

with LiveSQL Test Case:

and Chris said...

Use grouping_id.

Pass this the columns in your group by. It'll then return a numeric value.

The result is calculated from the binary value returned by concatenating the whether the current row is a subtotal (1) or not (0).

For example, the row:
ENT_NAME   ENT_TYPE   REC_COUNT   OCCUPIED_STORAGE(MB)  
Mgmt1      <null>           95600                   9144


Is a subtotal for ent_type, but not ent_name, which gives 01 in binary = 1 decimal

And
ENT_NAME   ENT_TYPE   REC_COUNT   OCCUPIED_STORAGE(MB)  
<null>     <null>          389000                  37860


Is a subtotal for both columns, giving 11 binary = 3 decimal.

Altogether this gives:

CREATE TABLE tab_ent_stats(
  ent_name VARCHAR2(100), ent_type VARCHAR2(100), rec_count NUMBER, occupied_storage NUMBER
);

INSERT INTO tab_ent_stats VALUES('Mgmt1','State1',18900,1786);
INSERT INTO tab_ent_stats VALUES('Mgmt1','State2',28900,2786);
INSERT INTO tab_ent_stats VALUES('Mgmt2','State1',38900,3786);
INSERT INTO tab_ent_stats VALUES('Mgmt2','State2',48900,4786);
INSERT INTO tab_ent_stats VALUES('Mgmt3','State1',58900,5786);

COMMIT;

SELECT ent_name, ent_type, 
       SUM(rec_count) AS rec_count, 
       SUM(occupied_storage) AS "OCCUPIED_STORAGE(MB)",
       grouping_id(ent_name, ent_type) grp
FROM tab_ent_stats
GROUP BY ROLLUP (ent_name,ent_type)
ORDER BY ent_name,ent_type;

ENT_NAME   ENT_TYPE   REC_COUNT   OCCUPIED_STORAGE(MB)   GRP   
Mgmt1      State1           37800                   3572     0 
Mgmt1      State2           57800                   5572     0 
Mgmt1      <null>           95600                   9144     1 
Mgmt2      State1           77800                   7572     0 
Mgmt2      State2           97800                   9572     0 
Mgmt2      <null>          175600                  17144     1 
Mgmt3      State1          117800                  11572     0 
Mgmt3      <null>          117800                  11572     1 
<null>     <null>          389000                  37860     3


All you need from here is a case/decode expression to convert these values to the names you want.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks

Sagar, February 14, 2018 - 6:28 pm UTC

Thanks Chris :)

Have a nice day!

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.