2

我有一张大桌子,如下所示:

section  term  percentageyr_1   percentageyr_2  percentageyr_3 ... percentageyr_25  
01          1         0.1              0.9           0.3                0.6     
01          2         0.3              0.5           0.01               0.3
01          3         0.6              0.4           0.2                0.6
01          4         0.3              0.5           0.7                0.14                  
01          5         0.4              0.2           0.6                0.6                           
            .
            .
            .
            .
            20

02          1         0.3              0.5           0.5                0.8
02          2         0.9              0.1           0                  0.6
.
.
.
20

我需要创建一个看起来更像以下的新表:

year | section | term | percent
--------------------------------
1         01       1       0.1
2         01       1       0.9
3         01       1       0.3
.         01       1        .
20        01       1       0.6
1         01       2       0.3
2         01       2       0.5
.         01       2       0.01
.         01       2
20        01       2       0.3
...
1         01       20
2         01       20   
.         01       20
.         01       20
20        01       20
1         02       1
2         02       1    
.         02       1
.         02       1
.         02       1
20        02       1 
etc.                           

我似乎有一些类似的示例使用 decode() 进行相反的转换,但我不确定列 -> 行方向以及如何更改转换中列的名称。

我真的很感激任何帮助!

4

2 回答 2

2

Is your goal your output or are you trying to transform your existing table? If your goal is your output above, try something like this:

SELECT r.r year, t.section, t.term
FROM LargeTable t, (
    SELECT Rownum r
    FROM dual
    CONNECT By Rownum <= 20 ) r
ORDER BY t.section, t.term, r.r

And here is the SQL Fiddle.

--EDIT

Given your edits, you could still use the following (I only used a couple columns, but you should get the idea), and add a CASE statement for your percentages:

SELECT r.r year, t.section, t.term,
   CASE 
      WHEN r.r = 1 THEN percentageyr_1
      WHEN r.r = 2 THEN percentageyr_2
      WHEN r.r = 25 THEN percentageyr_25
   END as percent
FROM LargeTable t, (
    SELECT Rownum r
    FROM dual
    CONNECT BY Rownum <= 25 ) r
    ORDER BY t.section, t.term, r.r

Here's the updated fiddle.

Good luck.

于 2013-01-27T01:09:26.173 回答
1

这种类型的转换实际上是一个unpivot。Oracle 10g 没有 unpivot 函数,但您可以使用 aUNION ALL将数据从列转换为行:

select percentageyr_1 year, section, term
from yourtable
union all
select percentageyr_2 year, section, term
from yourtable
union all
select percentageyr_3 year, section, term
from yourtable
union all
select percentageyr_25 year, section, term
from yourtable

请参阅带有演示的 SQL Fiddle

编辑,根据您的更改,您应该能够使用以下内容来取消透视数据:

select 1 year, section, term, percentageyr_1 percent
from yourtable
union all
select 2 year, section, term, percentageyr_2
from yourtable
union all
select 3 year, section, term, percentageyr_3
from yourtable
union all
select 25 year, section, term, percentageyr_25
from yourtable

请参阅带有演示的 SQL Fiddle

于 2013-01-27T01:06:29.553 回答