1

我有一个遗留表,我必须将其数据迁移到新结构。在旧结构中,每一行代表一年,每个月都有字段。在新的每一行将代表一个月。旧结构是:

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

) -> PK = id1,id2,年份

新结构是这样的:

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 回答