我需要查询以获取以下数据:
显示如下:
基本上为相应列中列出的每个值添加一个新的Vehicle_size列。例如。
kay_SM = Vehicle_size(Small)
kay_LG= Vehicle_size(Large)
Lola_SM= Vehicle_size(Small)
lola_LG= Vehicle_size(Large)
同时创建一个新列:竞争者,确定它是用于“ lola”还是“ kay”
答案 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