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

あなたの目標はあなたの出力ですか、それとも既存のテーブルを変換しようとしていますか?目標が上記の出力である場合は、次のようにしてみてください。

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

そして、これがSQLFiddleです。

- 編集

あなたの編集を考えると、あなたはまだ以下を使用することができます(私はいくつかの列だけを使用しましたが、あなたはアイデアを得る必要があります)、そしてCASEあなたのパーセンテージのステートメントを追加します:

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

これが更新されたフィドルです。

幸運を。

于 2013-01-27T01:09:26.173 に答える
1

このタイプの変換は、実際にはピボットされていません。Oracle 10gにはピボット解除機能はありませんが、を使用しUNION 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 FiddlewithDemoを参照してください

変更に基づいて編集し、以下を使用してデータのピボットを解除できるようにする必要があります。

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 FiddlewithDemoを参照してください

于 2013-01-27T01:06:29.553 に答える