Функции ROLLUP, CUBE, GROUPING и наборы GROUPING и функция ROW_NUMBER ()?
with
t as (
select
'Hagatna' as township,
'1' as pct,
373 as voters,
'1---(A-Z)' as precinct_inc,
'Guam Congress Building' as polling_location,
'Anigua' as landmark from dual union all
select
'Hagatna', '1', 373, '1---(A-Z)', 'Guam Congress Building', 'Anigua'
from dual union all
select 'Piti', '3', 441, '3---(A-Z)',
'Jose L.G. Rios Middle School Cafeteria',
'Nimitz Hill Estate' from dual union all
select 'Agat', '4', 688, '4---(A-D)',
'Marcial Sablan Elem. School Classrooms',
'Mt.Lamlam, Finile Beach' from dual union all
select 'Agat', '04A', 611, '4A-(E-P)',
'Marcial Sablan Elem. School Classrooms',
'Mt.Lamlam, Finile Beach' from dual union all
select 'Agat', '04B', 603, '4B-(Q-Z)',
'Marcial Sablan Elem. School Classrooms',
'Mt.Lamlam, Finile Beach' from dual union all
select 'Santa Rita', '5', 537, '5---(A-K)',
'Harry S. Truman Elem School Classrooms',
'Apra Hts., Naval Station, Santa Rosa' from dual union all
select 'Santa Rita', '05A',555, '5A-(L-Z)',
'Harry S. Truman Elem School Classrooms',
'Apra Hts., Naval Station, Santa Rosa' from dual union all
select 'Umatac', '6', 315, '6---(A-Z)', 'Umatac Mayor''s Office', 'n/a'
from dual union all
select 'Merizo', '7', 501, '7---(A-K)',
'Merizo Martyrs Memorial Elem School Cafeteria', 'n/a' from dual union all
select 'Merizo', '07A',531, '7A-(L-Z)',
'Merizo Martyrs Memorial Elem School Cafeteria', 'n/a' from dual union all
select 'Inarajan','8', 412, '8---(A-Fi)',
'Inarajan Middle School Classrooms', 'Malojloj' from dual union all
select 'Inarajan','08A',433, '8A-(Fj-Pa)',
'Inarajan Middle School Classrooms', 'Malojloj' from dual union all
select 'Inarajan','08B',383, '8B-(Pb-Z)',
'Inarajan Middle School Classrooms', 'Malojloj' from dual union all
select 'Talofofo','9',624, '9---(A-M)',
'Talofofo Elem. School Classrooms', 'Babulao, Ipan' from dual union all
select 'Talofofo','09A',589, '9A-(N-Z)',
'Talofofo Elem. School Classrooms', 'Babulao, Ipan' from dual union all
select 'Yona', '10', 395, '10--(A-E)',
'MU Lujan Elem. School Cafeteria', 'Baza Gardens, Togcha' from dual union all
select 'Yona', '10A', 424, '10A-(F-P)',
'MU Lujan Elem. School Cafeteria', 'Baza Gardens, Togcha' from dual union all
select 'Yona', '10B', 343, '10B-(Q-Z)',
'MU Lujan Elem. School Cafeteria', 'Baza Gardens, Togcha' from dual
)
select
case grouping(township)
when 1 then 'Total'
else case row_number() over (partition by township
order by grouping(pct), precinct_inc)
when 1 then township
else case grouping(pct)
when 1 then township||'''s Total'
else ' '
end
end
end as township,
pct, precinct_inc,
case row_number() over (partition by township
order by grouping(pct), precinct_inc)
when 1 then polling_location
else ' '
end as polling_location,
case row_number() over (partition by township
order by grouping(pct), precinct_inc)
when 1 then landmark
else ' '
end as landmark,
sum(voters)
from t
group by
rollup(township, (township, pct, precinct_inc, polling_location, landmark))
order by grouping(t.township), t.township,
grouping(t.pct), precinct_inc;
Вывод:
+--------------------+-----+--------------+-----------------------------------------------+--------------------------------------+-------------+
| TOWNSHIP | PCT | PRECINCT_INC | POLLING_LOCATION | LANDMARK | SUM(VOTERS) |
+--------------------+-----+--------------+-----------------------------------------------+--------------------------------------+-------------+
| Agat | 4 | 4---(A-D) | Marcial Sablan Elem. School Classrooms | Mt.Lamlam, Finile Beach | 688 |
| | 04A | 4A-(E-P) | | | 611 |
| | 04B | 4B-(Q-Z) | | | 603 |
| Agat's Total | | | | | 1902 |
| Hagatna | 1 | 1---(A-Z) | Guam Congress Building | Anigua | 746 |
| Hagatna's Total | | | | | 746 |
| Inarajan | 8 | 8---(A-Fi) | Inarajan Middle School Classrooms | Malojloj | 412 |
| | 08A | 8A-(Fj-Pa) | | | 433 |
| | 08B | 8B-(Pb-Z) | | | 383 |
| Inarajan's Total | | | | | 1228 |
| Merizo | 7 | 7---(A-K) | Merizo Martyrs Memorial Elem School Cafeteria | n/a | 501 |
| | 07A | 7A-(L-Z) | | | 531 |
| Merizo's Total | | | | | 1032 |
| Piti | 3 | 3---(A-Z) | Jose L.G. Rios Middle School Cafeteria | Nimitz Hill Estate | 441 |
| Piti's Total | | | | | 441 |
| Santa Rita | 5 | 5---(A-K) | Harry S. Truman Elem School Classrooms | Apra Hts., Naval Station, Santa Rosa | 537 |
| | 05A | 5A-(L-Z) | | | 555 |
| Santa Rita's Total | | | | | 1092 |
| Talofofo | 9 | 9---(A-M) | Talofofo Elem. School Classrooms | Babulao, Ipan | 624 |
| | 09A | 9A-(N-Z) | | | 589 |
| Talofofo's Total | | | | | 1213 |
| Umatac | 6 | 6---(A-Z) | Umatac Mayor's Office | n/a | 315 |
| Umatac's Total | | | | | 315 |
| Yona | 10 | 10--(A-E) | MU Lujan Elem. School Cafeteria | Baza Gardens, Togcha | 395 |
| | 10A | 10A-(F-P) | | | 424 |
| | 10B | 10B-(Q-Z) | | | 343 |
| Yona's Total | | | | | 1162 |
| Total | | | | | 9131 |
+--------------------+-----+--------------+-----------------------------------------------+--------------------------------------+-------------+
Протестируйте его онлайн с db <> fiddle .