你能给 UNPIVOT 的 FOR 子句传递 (2) 个参数吗? - 甲骨文 SQL
Can you pass (2) Arguments to the FOR clause of UNPIVOT? - ORACLE SQL
我正在尝试取消旋转一堆列:
基本上分为 3 列(旋转部分),Service_Area(例如 'CSA'、'ESA')和优先级(例如 'P1'、'P2')和 ActWklyHrs。我试图避免 UNPIVOT'ing 两次并且不得不添加一个 CTE(或者更糟的是,一个视图上的一个视图)并且我通过尝试通过两个争论(两个新专栏)来接近它:
CREATE OR REPLACE FORCE VIEW "STJOF"."vP3factPriorityDistribution" ("ISO_YearWeek", "ISO_Year", "ISO_Week", "Week_Start", "Service_Area", "DistSA_Key_Hash", "Priority", "ActWklyHrs")
AS
(
SELECT
CONCAT(TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IYYY'), TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IW')) AS ISO_YEARWEEK,
TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IYYY') AS ISO_YEAR,
TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IW') AS ISO_WEEK,
to_date(WEEK_START, 'MM/DD/YYYY') AS Week_Start,
SERVICE_AREA AS Service_Area,
ORA_HASH(SERVICE_AREA, 99999,1) AS DistSA_Key_Hash,
CASE "Priority"
WHEN 'P1' THEN 1
WHEN 'P2' THEN 2
WHEN 'P3' THEN 3
END AS "Priority",
ActWklyHrs AS ActWklyHrs
FROM "STJOF"."HistPriorityDistribution"
UNPIVOT ( (ActWklyHrs)
FOR "Service_Area", "Priority"
IN ( (CSA_P1) AS 'CSA','P1', (ESA_P1) AS 'ESA','P1', (PCA_P1) AS 'PCA','P1', (DCA_P1) AS 'DCA','P1', (SHA_P1) AS 'SHA','P1' (WSA_P1) AS 'WSA','P1' (WHA_P1) AS 'WHA','P1',
(CSA_P2) AS 'CSA','P2', (ESA_P2) AS 'ESA','P2', (PCA_P2) AS 'PCA','P2', (DCA_P2) AS 'DCA','P2', (SHA_P2) AS 'SHA','P2' (WSA_P2) AS 'WSA','P2' (WHA_P2) AS 'WHA','P2',
(CSA_P3) AS 'CSA','P3', (ESA_P3) AS 'ESA','P3', (PCA_P3) AS 'PCA','P3', (DCA_P3) AS 'DCA','P3', (SHA_P3) AS 'SHA','P3' (WSA_P3) AS 'WSA','P3' (WHA_P3) AS 'WHA','P3'
)
)
);
是否可以不使用 CTE 和 UNPIVOT 两次?这是错误的方法吗?
为什么不直接 substr()
?
select week_start, substr(str, 1, 3) as service, substr(str, 4) as priority, hrs
from test
unpivot (hrs for str in (CSA_P1, ESA_P1, CSA_P2, ESA_P2, CSA_P3, ESA_P3))
您也可以在 unpivot
子句中执行所有操作:
select *
from test
unpivot (hrs for (service, priority) in (CSA_P1 as ('CSA', 'P1'),
ESA_P1 as ('ESA', 'P1'),
CSA_P2 as ('CSA', 'P2'),
ESA_P2 as ('ESA', 'P2'),
CSA_P3 as ('CSA', 'P3'),
ESA_P3 as ('ESA', 'P3') ) )
FOR ("Service_Area", "Priority") --PARANTHESIS WERE NEEDED HERE
IN ( (CSA_P1) AS ('CSA','P1'), (ESA_P1) AS ('ESA','P1'), (PCA_P1) AS ('PCA','P1'), (DCA_P1) AS ('DCA','P1'), (SHA_P1) AS ('SHA','P1'), (WSA_P1) AS ('WSA','P1'), (WHA_P1) AS ('WHA','P1') --AND AFTER EACH 'AS',
(CSA_P2) AS ('CSA','P2'), (ESA_P2) AS ('ESA','P2'), (PCA_P2) AS ('PCA','P2'), (DCA_P2) AS ('DCA','P2'), (SHA_P2) AS ('SHA','P2'), (WSA_P2) AS ('WSA','P2'), (WHA_P2) AS ('WHA','P2'),
(CSA_P3) AS ('CSA','P3'), (ESA_P3) AS ('ESA','P3'), (PCA_P3) AS ('PCA','P3'), (DCA_P3) AS ('DCA','P3'), (SHA_P3) AS ('SHA','P3'), (WSA_P3) AS ('WSA','P3'), (WHA_P3) AS ('WHA','P3')
)
```` )
我正在尝试取消旋转一堆列:
基本上分为 3 列(旋转部分),Service_Area(例如 'CSA'、'ESA')和优先级(例如 'P1'、'P2')和 ActWklyHrs。我试图避免 UNPIVOT'ing 两次并且不得不添加一个 CTE(或者更糟的是,一个视图上的一个视图)并且我通过尝试通过两个争论(两个新专栏)来接近它:
CREATE OR REPLACE FORCE VIEW "STJOF"."vP3factPriorityDistribution" ("ISO_YearWeek", "ISO_Year", "ISO_Week", "Week_Start", "Service_Area", "DistSA_Key_Hash", "Priority", "ActWklyHrs")
AS
(
SELECT
CONCAT(TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IYYY'), TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IW')) AS ISO_YEARWEEK,
TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IYYY') AS ISO_YEAR,
TO_CHAR(to_date(WEEK_START,'MM/DD/YYYY'),'IW') AS ISO_WEEK,
to_date(WEEK_START, 'MM/DD/YYYY') AS Week_Start,
SERVICE_AREA AS Service_Area,
ORA_HASH(SERVICE_AREA, 99999,1) AS DistSA_Key_Hash,
CASE "Priority"
WHEN 'P1' THEN 1
WHEN 'P2' THEN 2
WHEN 'P3' THEN 3
END AS "Priority",
ActWklyHrs AS ActWklyHrs
FROM "STJOF"."HistPriorityDistribution"
UNPIVOT ( (ActWklyHrs)
FOR "Service_Area", "Priority"
IN ( (CSA_P1) AS 'CSA','P1', (ESA_P1) AS 'ESA','P1', (PCA_P1) AS 'PCA','P1', (DCA_P1) AS 'DCA','P1', (SHA_P1) AS 'SHA','P1' (WSA_P1) AS 'WSA','P1' (WHA_P1) AS 'WHA','P1',
(CSA_P2) AS 'CSA','P2', (ESA_P2) AS 'ESA','P2', (PCA_P2) AS 'PCA','P2', (DCA_P2) AS 'DCA','P2', (SHA_P2) AS 'SHA','P2' (WSA_P2) AS 'WSA','P2' (WHA_P2) AS 'WHA','P2',
(CSA_P3) AS 'CSA','P3', (ESA_P3) AS 'ESA','P3', (PCA_P3) AS 'PCA','P3', (DCA_P3) AS 'DCA','P3', (SHA_P3) AS 'SHA','P3' (WSA_P3) AS 'WSA','P3' (WHA_P3) AS 'WHA','P3'
)
)
);
是否可以不使用 CTE 和 UNPIVOT 两次?这是错误的方法吗?
为什么不直接 substr()
?
select week_start, substr(str, 1, 3) as service, substr(str, 4) as priority, hrs
from test
unpivot (hrs for str in (CSA_P1, ESA_P1, CSA_P2, ESA_P2, CSA_P3, ESA_P3))
您也可以在 unpivot
子句中执行所有操作:
select *
from test
unpivot (hrs for (service, priority) in (CSA_P1 as ('CSA', 'P1'),
ESA_P1 as ('ESA', 'P1'),
CSA_P2 as ('CSA', 'P2'),
ESA_P2 as ('ESA', 'P2'),
CSA_P3 as ('CSA', 'P3'),
ESA_P3 as ('ESA', 'P3') ) )
FOR ("Service_Area", "Priority") --PARANTHESIS WERE NEEDED HERE
IN ( (CSA_P1) AS ('CSA','P1'), (ESA_P1) AS ('ESA','P1'), (PCA_P1) AS ('PCA','P1'), (DCA_P1) AS ('DCA','P1'), (SHA_P1) AS ('SHA','P1'), (WSA_P1) AS ('WSA','P1'), (WHA_P1) AS ('WHA','P1') --AND AFTER EACH 'AS',
(CSA_P2) AS ('CSA','P2'), (ESA_P2) AS ('ESA','P2'), (PCA_P2) AS ('PCA','P2'), (DCA_P2) AS ('DCA','P2'), (SHA_P2) AS ('SHA','P2'), (WSA_P2) AS ('WSA','P2'), (WHA_P2) AS ('WHA','P2'),
(CSA_P3) AS ('CSA','P3'), (ESA_P3) AS ('ESA','P3'), (PCA_P3) AS ('PCA','P3'), (DCA_P3) AS ('DCA','P3'), (SHA_P3) AS ('SHA','P3'), (WSA_P3) AS ('WSA','P3'), (WHA_P3) AS ('WHA','P3')
)
```` )