我想采取以下查询
select '2009042 Restraint 151214.pdf',
'2009042 Restraint 170215.pdf',
'2009042 Restraint 240215.pdf',
'2009856 Restraint 190215.pdf',
'208272 Notice 120215.pdf',
'208272 Restraint 120215.pdf',
'212598 Restraint 160215.pdf',
'213195 Notice 130215.pdf'
from dual
并将其转换为一个查询,该查询将上述查询中的列作为行返回。 pivot声明似乎无法做到这一点。
所以我想返回行:
COL1
2009042 Restraint 151214.pdf
2009042 Restraint 170215.pdf
2009042 Restraint 240215.pdf
2009856 Restraint 190215.pdf
208272 Notice 120215.pdf
208272 Restraint 120215.pdf
212598 Restraint 160215.pdf
213195 Notice 130215.pdf
略有不同
答案 0 :(得分:2)
枢轴声明似乎无法做到这一点。
是的,因为您并未尝试将行转移到列中,而是在尝试将从列中取消 行。
如果您的列具有预定义的枚举名称,则可以使用UNPIVOT
,如下所示:
select *
from (
select '2009042 Restraint 151214.pdf' v1,
'2009042 Restraint 170215.pdf' v2,
'2009042 Restraint 240215.pdf' v3,
'2009856 Restraint 190215.pdf' v4,
'208272 Notice 120215.pdf' v5,
'208272 Restraint 120215.pdf' v6,
'212598 Restraint 160215.pdf' v7,
'213195 Notice 130215.pdf' v8
from dual
) t
unpivot (
col1 for v in (v1, v2, v3, v4, v5, v6, v7, v8)
)
产量:
V COL1
--------------------------------
V1 2009042 Restraint 151214.pdf
V2 2009042 Restraint 170215.pdf
V3 2009042 Restraint 240215.pdf
V4 2009856 Restraint 190215.pdf
V5 208272 Notice 120215.pdf
V6 208272 Restraint 120215.pdf
V7 212598 Restraint 160215.pdf
V8 213195 Notice 130215.pdf
如果没有这样的名称可用,事情会变得多毛,但仍然可行(可能取决于Oracle中没有明确定义的列命名约定):
select *
from (
select '2009042 Restraint 151214.pdf',
'2009042 Restraint 170215.pdf',
'2009042 Restraint 240215.pdf',
'2009856 Restraint 190215.pdf',
'208272 Notice 120215.pdf',
'208272 Restraint 120215.pdf',
'212598 Restraint 160215.pdf',
'213195 Notice 130215.pdf'
from dual
) t
unpivot (
col1 for v in (
"'2009042RESTRAINT151214.PDF'",
"'2009042RESTRAINT170215.PDF'",
"'2009042RESTRAINT240215.PDF'",
"'2009856RESTRAINT190215.PDF'",
"'208272RESTRAINT120215.PDF'",
"'208272NOTICE120215.PDF'",
"'212598RESTRAINT160215.PDF'",
"'213195NOTICE130215.PDF'"
)
)
产量:
V COL1
------------------------------------------------------------
'2009042RESTRAINT151214.PDF' 2009042 Restraint 151214.pdf
'2009042RESTRAINT170215.PDF' 2009042 Restraint 170215.pdf
'2009042RESTRAINT240215.PDF' 2009042 Restraint 240215.pdf
'2009856RESTRAINT190215.PDF' 2009856 Restraint 190215.pdf
'208272RESTRAINT120215.PDF' 208272 Restraint 120215.pdf
'208272NOTICE120215.PDF' 208272 Notice 120215.pdf
'212598RESTRAINT160215.PDF' 212598 Restraint 160215.pdf
'213195NOTICE130215.PDF' 213195 Notice 130215.pdf
当然,如果您准备编写那么多SQL,为什么不使用UNION ALL
,as Gordon suggested
答案 1 :(得分:1)
首先将查询短语称为联合:
select '2009042 Restraint 151214.pdf' from dual union all
select '2009042 Restraint 170215.pdf' from dual union all
select '2009042 Restraint 240215.pdf' from dual union all
select '2009856 Restraint 190215.pdf' from dual union all
select '208272 Notice 120215.pdf' from dual union all
select '208272 Restraint 120215.pdf' from dual union all
select '212598 Restraint 160215.pdf' from dual union all
select '213195 Notice 130215.pdf' from dual
答案 2 :(得分:0)
快速执行此操作的方法是使用以下Oracle system defined type,sys.odcivarchar2list
:
select column_value col1
from table(sys.odcivarchar2list(
'2009042 Restraint 151214.pdf',
'2009042 Restraint 170215.pdf',
'2009042 Restraint 240215.pdf',
'2009856 Restraint 190215.pdf',
'208272 Notice 120215.pdf',
'208272 Restraint 120215.pdf',
'212598 Restraint 160215.pdf',
'213195 Notice 130215.pdf'));
返回:
COL1
--------------------------------------------------------------------------------
2009042 Restraint 151214.pdf
2009042 Restraint 170215.pdf
2009042 Restraint 240215.pdf
2009856 Restraint 190215.pdf
208272 Notice 120215.pdf
208272 Restraint 120215.pdf
212598 Restraint 160215.pdf
213195 Notice 130215.pdf
如果你想使用数值,那么sys.odcinumberlist()
,以及上面链接的Oracle doc中记录的其他数据。