[英]Oracle Sql Query to rollup but get main column to display only once
PREVIOUS DATA WHICH I USED我使用的以前的数据
TOWNSHIP PCT TOTAL
-------------------- --- ----------
Agat 4 688
Agat 04A 611
Agat 04B 603
Agat 1902
Piti 3 441
Piti 441
Yigo 19 376
Yigo 19A 405
Yigo 19B 465
Yigo 19C 418
Yigo 19D 353
Yigo 19E 373
Yigo 19F 318
Yigo 2708
Yona 10 395
Yona 10A 424
Yona 10B 343
NEW DATA LOOKS LIKE THIS新数据看起来像这样
with tab 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 Mayors 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 )
This is how my data looks like now i dont want to display TOWNSHIP column more than once how to achieve this in sql?这就是我的数据现在的样子我不想多次显示 TOWNSHIP 列如何在 sql 中实现这一点?
It should like below any idea on how to achieve this.它应该喜欢下面关于如何实现这一点的任何想法。
TOWNSHIP PCT TOTAL
-------------------- --- ----------
Agat 4 688
04A 611
04B 603
Total 1902
MY QUERY:
COL VILLAGE FOR A20
SET PAGESIZE 50000
set lines 154
--grouping(district), grouping(pct)
select NVL(DISTRICT,'')
district, pct,
sum(VOTERS) as TOTAL FROM
(SELECT distinct DISTRICT,
PCT,
COUNT(*) AS VOTERS
FROM
REG_TAB
JOIN PCT_LOOKUP
ON
PCT=PERCINCT_MAP
AND VILLAGE IN (UPPER(SUBSTR(DISTRICT,1,INSTR(DISTRICT,'/',1)-1)),UPPER(
SUBSTR(REPLACE(DISTRICT,'.',''),INSTR(DISTRICT,'/',1)+1,99))) GROUP BY
PCT,DISTRICT ORDER BY to_number(substr(PCT,1,length(PCT)-
nvl(length(replace(translate(PCT,'0123456789','0000000000'),'0','')),0))),
substr(PCT,1+length(PCT)-
nvl(length(replace(translate(PCT,'0123456789','0000000000'),'0','')),0))
NULLS FIRST) group by ROLLUP(district, pct) ;
I want to get like above table i tried rollup but that brings the column name but i dont want that column name to be displayed multiple times instead once is enough.我想像上表一样尝试汇总,但这带来了列名,但我不希望该列名显示多次,而一次就足够了。
Just figuring out how to achieve this.只是想知道如何实现这一目标。
ROLLUP, CUBE, GROUPING Functions and GROUPING SETS and the ROW_NUMBER() function? ROLLUP、CUBE、GROUPING 函数和 GROUPING SETS以及 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;
Output:输出:
+--------------------+-----+--------------+-----------------------------------------------+--------------------------------------+-------------+
| 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 |
+--------------------+-----+--------------+-----------------------------------------------+--------------------------------------+-------------+
Test it online with db<>fiddle .使用db<>fiddle在线测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.