您可以将 (2) 参数传递给 UNPIVOT 的 FOR 子句吗? - 甲骨文SQL

Posted

技术标签:

【中文标题】您可以将 (2) 参数传递给 UNPIVOT 的 FOR 子句吗? - 甲骨文SQL【英文标题】:Can you pass (2) Arguments to the FOR clause of UNPIVOT? - ORACLE SQL 【发布时间】:2020-06-16 18:35:28 【问题描述】:

我正在尝试取消旋转一堆列:

基本上分为 3 列(旋转部分),Service_Area(例如“CSA”、“ESA”)和 Priority(例如“P1”、“P2”)和 ActWklyHrs。我试图避免 UNPIVOT 两次并且不得不添加一个 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 两次?这是错误的方法吗?

【问题讨论】:

【参考方案1】:

为什么不直接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') ) )

dbfiddle

【讨论】:

所以,我认为我完全按照您对 unpivot 代码的建议执行了操作,但您通过提供正确的语法间接回答了我的问题。似乎问题在于 FOR 子句的 (2) 参数周围缺少一组括号,以及 IN 子句中它们的值周围的相应组。所以答案是,是的,您可以将两个论点传递给 UNPIVOT 的 FOR 子句。【参考方案2】:
              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')
                 ) 
````          )

【讨论】:

以上是关于您可以将 (2) 参数传递给 UNPIVOT 的 FOR 子句吗? - 甲骨文SQL的主要内容,如果未能解决你的问题,请参考以下文章

如何将参数传递给 Java 线程?

您可以在创建时将参数传递给 AngularJS 控制器吗?

您可以在创建时将参数传递给 AngularJS 控制器吗?

使用它时将参数传递给perl包

你如何将参数传递给define_method?

通过 UI 将参数传递给 Airflow 的作业