Oracle Sql Query进行汇总,但使主列仅显示一次

时间:2019-02-20 17:58:21

标签: sql oracle

我使用过的上次数据

 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

与此类似的新数据外观

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 ) 

这是我的数据的样子,现在我不想多次显示TOWNSHIP列,如何在sql中实现呢?

下面应该对如何实现这一目标有任何想法。

  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) ;

我想像上表一样尝试汇总,但这带来了列名,但是我不希望该列名显示多次,而一次显示就足够了。

只需弄清楚如何做到这一点。

1 个答案:

答案 0 :(得分:1)

ROLLUP, CUBE, GROUPING Functions and 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;

输出:

+--------------------+-----+--------------+-----------------------------------------------+--------------------------------------+-------------+
|      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在线进行测试。