Oracle:分解数据

时间:2019-02-27 22:37:07

标签: oracle

我需要查询以获取以下数据:

enter image description here

显示如下:

enter image description here

基本上为相应列中列出的每个值添加一个新的Vehicle_size列。例如。

kay_SM = Vehicle_size(Small)
kay_LG= Vehicle_size(Large)
Lola_SM= Vehicle_size(Small)
lola_LG= Vehicle_size(Large)

同时创建一个新列:竞争者,确定它是用于“ lola”还是“ kay”

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以使用unpivot

SELECT U.RENTAL_DATE,U.OUTBOUND,U.INBOUND,
REGEXP_SUBSTR(VEHICLE_COMPETITIOR, '[^_ ]+', 1, 2) AS VEHICLE_SIZE,
REGEXP_SUBSTR(VEHICLE_COMPETITIOR, '[^_ ]+', 1, 1) AS COMPETITIOR,
RATE 
FROM T
UNPIVOT (
          RATE FOR VEHICLE_COMPETITIOR IN (KAY_SM  AS 'Kay_Small',
          KAY_LG  AS 'Kay_Large',LOLA_SM AS 'Lola_Small' ,LOLA_LG AS 'Lola_Large')
        ) U