1

新しい構造に移行する必要のあるデータを持っているレガシーテーブルがあります。古い構造では、各行は1年を表し、各月のフィールドがあります。新しい行では、各行が1か月を表します。古い構造は次のとおりです。

OLD_TABLE (
id1,
id2,
    year,
price01,
price02,
price03,
price04,
...
price11,
price12,
quantity01,
quantity02,
quantity03,
...
quantity11,
quantity12,

)-> PK = id1、id2、year

新しい構造は次のようになります:

NEW_TABLE (
id1,
id2,
year,
month,
quantity,
price,

)-> PK = id1、id2、年

古い構造から新しい構造へのデータの移行をどのように進める必要があるかについてのアドバイスはありますか?

ありがとうございました!

4

2 に答える 2

3

11g 以降を使用している場合は、unpivot構文を確認してください。

何かのようなもの...

SELECT *
FROM yourtable
UNPIVOT (price FOR pmonth IN (price01 AS 1, price02 AS 2...))
UNPIVOT (quantity FOR qmonth IN (quantity01 AS 1, quantity02 AS 2...))
WHERE pmonth=qmonth
于 2012-08-13T07:59:51.687 に答える
1
SELECT id1, id2, year, 1 as month, quantity01 as quantity, price01 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 2 as month, quantity02 as quantity, price02 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 3 as month, quantity03 as quantity, price03 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 4 as month, quantity04 as quantity, price04 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 5 as month, quantity05 as quantity, price05 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 6 as month, quantity06 as quantity, price06 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 7 as month, quantity07 as quantity, price07 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 8 as month, quantity08 as quantity, price08 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 9 as month, quantity09 as quantity, price09 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 10 as month, quantity010 as quantity, price010 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 11 as month, quantity011 as quantity, price011 as price FROM old_table
UNION ALL
SELECT id1, id2, year, 12 as month, quantity012 as quantity, price012 as price FROM old_table
;
于 2012-08-13T08:15:08.923 に答える