不使用列名称取消透视

时间:2018-01-26 18:06:47

标签: sql oracle unpivot

现在我的查询结果是以下格式生成的数据。

dbid       askid         amid
================================
d1           m1            a1

我想将显示更改为

SOURCE_ID    DEST_ID
========================
m1             d1
a1             d1

我们不能使用union all,内连接,交叉连接,因为实时它是一个非常大的查询并与n个多个表交互并且它已经有很多no.of 我尝试使用unpivot,但每次将列名存储到行中时。

现在生成数据的示例查询:

 select DEST_ID,SOURCE_ID from 
    (select        
    t1.id as dbid,
    t2.mid as askid,
    t3.m2idd as amid from
    table1 t1, table2 t2, table3 t3 where
    t1.actid = t2.senid
    and t2.denid = t2.mkid
    )
    unpivot INCLUDE NULLS (SOURCE_ME_GUID FOR DEST_ME_GUID IN (amid, askid));

2 个答案:

答案 0 :(得分:0)

UNION ALLWITH子句结合使用。

你以某种方式对你的加入标准感到困惑。我认为mkiddenid位于table3。否则你就会加入table3。你不应该使用这个20世纪80年代的连接语法; 1992年因为某种原因而被裁掉了。

with ids as
(
  select t1.id as dbid, t2.mid as askid, t3.m2idd as amid 
 from table1 t1
 join table2 t2 on t2.senid = t1.actid
 join table3 t3 on t3.mkid = t2.denid
)
select askid as source_id, dbid as dest_id from ids
union all
select amid as source_id, dbid as dest_id from ids;

答案 1 :(得分:0)

解决上述问题。

 select dbid as DEST_ID, SOURCE_ID 
from ( 
 select 'd1' dbid, 
 cast('m1' as varchar2(200)) as askid, 
 cast('a1' as varchar2(200)) as amid 
 from dual 
) unpivot INCLUDE NULLS ( 
 SOURCE_ID FOR DEST_ID IN (amid, askid) 
);