I am writing Oracle SQL query; where i need to compute subtotals along with my normal query results.
For this i decided to use GROUP BY
Extension (GROUPING SETS)
Due to privacy issues i am not sharing actual query. Instead i am writing below an imaginary query which best describe my subtotal requirement
SELECT
region,
country,
province,
city,
SUM(agg_col1) agg_col1,
SUM(agg_col2) agg_col2
FROM tbl_name
GROUP BY GROUPING SETS(region)
Now i know above query will give an error because not all non-aggregate columns are part of GROUP BY
expression; but this is the actual point where i got stuck and i am clue less how to achieve desired results. My only requirement is to get region wise subtotals and grand toal along with query results.
I will also appreciate if some detailed reading material or video is recommended for mastering this.
EDIT Suppose below is some sample data I want the sum of regions e.g consider below data Region Country Province City Agg 1 Agg 2
r1 c1 p1 ct1 1 1
r1 c1 p1 ct2 1 1
r1 c1 p2 ct1 1 1
r1 c1 p2 ct2 1 1
r1 c2 p1 ct1 1 1
r1 c2 p1 ct2 1 1
r1 c2 p2 ct1 1 1
r1 c2 p2 ct2 1 1
r2 c1 p1 ct1 1 1
r2 c1 p1 ct2 1 1
r2 c1 p2 ct1 1 1
r2 c1 p2 ct2 1 1
r2 c2 p1 ct1 1 1
r2 c2 p1 ct2 1 1
r2 c2 p2 ct1 1 1
r2 c2 p2 ct2 1 1
Along with above 16 rows i need following 3 rows
r1 8 8
r2 8 8
16 16