Sql 取消PIVOT查询连接到其他表
我有一个问题如下Sql 取消PIVOT查询连接到其他表,sql,oracle,Sql,Oracle,我有一个问题如下 with t as ( select ID, name, tag, tag_1, tag_2, tag_3, tag_4, location from table_one ) select * from t unpivot ( value for _tag_ in (tag,tag_1,tag_2,tag_3,tag_4) ) 现在,我想将其他3个表table1、table2、table3连接到上面,我需要从这些表中
with t as (
select ID, name, tag, tag_1, tag_2, tag_3, tag_4, location from table_one
)
select * from t
unpivot (
value for _tag_ in (tag,tag_1,tag_2,tag_3,tag_4)
)
现在,我想将其他3个表table1、table2、table3连接到上面,我需要从这些表中选择其他列,例如col1、col2、col3。你知道怎么做吗 我会在Oracle 12C+中使用横向联接:
select u.*
from t cross apply
(select id, name, tag from dual union all
select id, name, tag_1 from dual union all
select id, name, tag_2 from dual union all
select id, name, tag_3 from dual union all
select id, name, tag_4 from dual
) u;
然后,您可以加入u
,就像您加入其他任何内容一样:
select u.*, . . .
from t cross apply
(select id, name, tag from dual union all
select id, name, tag_1 from dual union all
select id, name, tag_2 from dual union all
select id, name, tag_3 from dual union all
select id, name, tag_4 from dual
) u join
x
on u.? = x.?;
在Oracle 11中,如果将
unpivot
作为子查询或CTE,则可以执行类似的操作。只需从t中选择*即可连接其他表-您也可以将“t”作为子查询,而不必创建正式的CTE。您使用的Oracle版本是什么?