Trouble with the pivot and the columns that contain numbers/spaces

Hello

I have trouble with the pivot statement.
How could I do this just for columns that are numbers?
How could I do this just for columns that contain spaces?

Can someone please help?


based on the documentation of Carsten Czarski
http://SQL-PLSQL-de.blogspot.com/2007/08/kreuztabellen-in-Oracle11g-SQL-pivot.html

essentially to help:
--------------------
Select deptno, sum (clerk), sum (salesman), sum (manager)
of (emp) pivot
Sum (SAL) of EMPLOYMENT
in ("CLERK" as a 'CLERK', 'SELLER' as 'SELLER', 'MANAGER' as 'MANAGER')
)
Deptno group
--------------------
DEPTNO SUM (CLERK) SUM (SALESMAN) SUM (MANAGER)
---------- ---------- ------------- ------------
30 950 5600 2850
20 1900 2975
10-1300-2450



I tried to run at my own table:

NAME MONAT WERT
-------------------------------------------------- ---------- ----------
5 Antarctica 404,84
Asia 7 106,41
Oceana 2 456,96
4 the Europe 426,23
9 Antarctic 537,56
Europe 9 832,58
The South America 12 662,41
Europe 4 422,27
America of the North 7 312,19
America of the North 10 148,92

10 selected lines.


But running:
--------------------
SELECT name, sum (1), sum (2), sum (3), sum (4), sum (5), sum (6), sum (7), sum (8), sum (9), sum (10), sum (11), sum (12)
(pivot) apex_wksp.demo_pivot2
Sum (Wert) for monat
in ('1 ', '2', ' 3', '4 ', '5', '6', '7',' 8 ', ' 9',' 10', ' 11 ', ' 12')
)
Group by name
;
--------------------
led to:
Antarctica 1 2 3 4 5 6 7 8 9 10 11 12
North America 1 2 3 4 5 6 7 8 9 10 11 12
Oceana 1 2 3 4 5 6 7 8 9 10 11 12
The South America 1 2 3 4 5 6 7 8 9 10 11 12
Europe 1 2 3 4 5 6 7 8 9 10 11 12
Asia 1 2 3 4 5 6 7 8 9 10 11 12
6 selected lines.


not quite what I expected.





In addition,
--------------------
Select monat, sum (Antarctica), sum (North America), sum (Oceana), sum (South America), sum (Europe), sum (Asia)
(pivot) apex_wksp.demo_pivot2
Sum (Wert) name
("Antarctic", "North America", "Oceana", "South America", "Europe", "Asia")
)
Group by name
;
---------------------
ORA 907 results



I know what the problem is - but how do I do it right?


using double quotes or replace (ing) space with just underscores seems to garble sql for the unreadable.




Help, please.



Thanks in advance,
Michael Weinberger

Attention to the rotated default column alias:

SQL> with demo_pivot2 as (
  2                       select 'Antarctica' name,5 monat,404.84 wert from dual union all
  3                       select 'Asia',7,106.41 from dual union all
  4                       select 'Oceana',2,456.96 from dual union all
  5                       select 'Europe',4,426.23 from dual union all
  6                       select 'Antarctica',9,537.56 from dual union all
  7                       select 'Europe',9,832.58 from dual union all
  8                       select 'South America',12,662.41 from dual union all
  9                       select 'Europe',4,422.27 from dual union all
 10                       select 'North America',7,312.19 from dual union all
 11                       select 'North America',10,148.92 from dual
 12                      )
 13  select name, sum("'1'"),sum("'2'"),sum("'3'"),sum("'4'"),sum("'5'"),sum("'6'"),sum("'7'"),sum("'8'"),sum("'9'"),sum("'10'"),sum("'11'"),sum("'12'")
 14  from demo_pivot2 PIVOT (
 15  sum(wert) for monat
 16  in ('1','2','3','4','5','6','7','8','9','10','11','12')
 17  )
 18  group by name
 19  ;

NAME          SUM("'1'") SUM("'2'") SUM("'3'") SUM("'4'") SUM("'5'") SUM("'6'") SUM("'7'") SUM("'8'") SUM("'9'") SUM("'10'") SUM("'11'") SUM("'12'")
------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------- ----------- -----------
Antarctica                                                    404.84                                      537.56
North America                                                                       312.19                            148.92
Oceana                       456.96
South America                                                                                                                                 662.41
Europe                                              848.5                                                 832.58
Asia                                                                                106.41

6 rows selected.

SQL> 

SY.

Tags: Database

Similar Questions

Maybe you are looking for