Oracle11g提供了新的行列轉(zhuǎn)換操作:PIVOT(行轉(zhuǎn)列)和UNPIVOT列轉(zhuǎn)行伪朽。老版本使用decode來(lái)進(jìn)行轉(zhuǎn)換瞧挤,本文使用UNPIVOT的功能進(jìn)行展示龄砰。 詳細(xì)資料請(qǐng)看: http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html
現(xiàn)有表src_table如下:
product_Id(產(chǎn)品ID)product_color(顏色)porduct_type(型號(hào))is_intelligent(是否智能)
1111 ? ? ?red ? ? t1 ? ? 是
1112 ? ? ?blue ? t2 ? ? ?否
1113 ? ? green ?t3 ? ? 是
目標(biāo)表dest_table如下:
product_Idparam_name(參數(shù)名稱)param_value(參數(shù)值)
1111 ? product_color ? ? red
1112 ? product_color ? ?blue
1113 ? product_color ? ? green
1111 ? product_typet ? ?1
1112 ? ?product_typet ? 2
1113 ? ?product_typet ? 3
1111 ? is_intelligent ? ?是
1112 ? is_intelligent ? ?否
1113 ? is_intelligent ? ?是
通過(guò)UNPIVOT實(shí)現(xiàn)如下:
SELECT *
FROM?? src_table
UNPIVOT (param_value FOR param_name IN (product_color AS 'product_color', product_type AS 'product_type', is_intelligent AS 'is_intelligent');