在oracle中将数据列成行

Columns data into rows in oracle

数据以 table 格式显示在屏幕截图中。我想转换成如下所述的所需格式。

Table 描述:

Table A
(branch_code, branch_name, branch_state, hol1, hol2, hol3....hol100)

预期输出

TAMF046  14/01/2021
TAMF046  15/01/2021
TAMF046  26/01/2021
KERF047  26/01/2021
KERF047  11/03/2021
KERF047  02/04/2021

使用联合

例如:

select Branch_Code, Hol1 from MyTable
union
select Branch_Code, Hol2 from MyTable

等等

这正是 UNPIVOT 的目的

with t(id, c1, c2, c3) as (
  select 1, 'a', 'b', 'c' from dual union all
  select 2, 'aa', 'bb', 'cc' from dual
)
select *
from t
unpivot (
  val
  for col in (
    c1 as 'A1',
    c2 as 'A2',
    c3 as 'A3'
  )
)

val 是新列,它将包含列 c1 c2 c3.

中的值

col 是新列,它将包含 val 来自的列的名称。

A1 A2 A3 是您要在 col 中为每个未透视的列填充的值(如果您对原始列名称)。

 WITH HOLIDAY 
AS ( 
    SELECT BRANCH_CODE,HOL1,HOL2,HOL3,HOL4,HOL5,HOL6,HOL7,HOL8,HOL9,HOL10,HOL11,HOL12,HOL13,HOL14,HOL15,HOL16,HOL17,HOL18,HOL19,HOL20,HOL21,HOL22,HOL23,HOL24,HOL25,HOL26,HOL27,HOL28,HOL29,HOL30,HOL31,HOL32,HOL33,HOL34,HOL35,HOL36,HOL37,HOL38,HOL39,HOL40,HOL41,HOL42,HOL43,HOL44,HOL45,HOL46,HOL47,HOL48,HOL49,HOL50,HOL51,HOL52,HOL53,HOL54,HOL55,HOL56,HOL57,HOL58,HOL59,HOL60,HOL61,HOL62,HOL63,HOL64,HOL65,HOL66,HOL67,HOL68,HOL69,HOL70,HOL71,HOL72,HOL73,HOL74,HOL75,HOL76,HOL77,HOL78,HOL79,HOL80,HOL81,HOL82,HOL83,HOL84,HOL85,HOL86,HOL87,HOL88,HOL89,HOL90,HOL91,HOL92,HOL93,HOL94,HOL95,HOL96,HOL97,HOL98,HOL99,HOL100 
      FROM CUST_HOLIDAY_MASTER 
      WHERE BRANCH_CODE = I.BRANCH_CODE
    )
SELECT  BRANCH_CODE , COLVALUE FROM abc

unpivot
( colvalue for col in (HOL1,HOL2,HOL3,HOL4,HOL5,HOL6,HOL7,HOL8,HOL9,HOL10,HOL11,HOL12,HOL13,HOL14,HOL15,HOL16,HOL17,HOL18,HOL19,HOL20,HOL21,HOL22,HOL23,HOL24,HOL25,HOL26,HOL27,HOL28,HOL29,HOL30,HOL31,HOL32,HOL33,HOL34,HOL35,HOL36,HOL37,HOL38,HOL39,HOL40,HOL41,HOL42,HOL43,HOL44,HOL45,HOL46,HOL47,HOL48,HOL49,HOL50,HOL51,HOL52,HOL53,HOL54,HOL55,HOL56,HOL57,HOL58,HOL59,HOL60,HOL61,HOL62,HOL63,HOL64,HOL65,HOL66,HOL67,HOL68,HOL69,HOL70,HOL71,HOL72,HOL73,HOL74,HOL75,HOL76,HOL77,HOL78,HOL79,HOL80,HOL81,HOL82,HOL83,HOL84,HOL85,HOL86,HOL87,HOL88,HOL89,HOL90,HOL91,HOL92,HOL93,HOL94,HOL95,HOL96,HOL97,HOL98,HOL99,HOL100) 
)

WHERE COLVALUE IS NOT NULL;

这是我尝试的方法,但如果有人有任何其他使用动态查询的方法。请分享您的观点,