将列移到行

时间:2019-04-22 13:33:24

标签: sql oracle

我需要在第一列中添加第三列(我希望第一列还将包括第三列)

当前状态:

Current Status

所需结果:

desire

4 个答案:

答案 0 :(得分:4)

您想要UNION ALL

SELECT t.entity, t.activity
FROM table t
UNION ALL
SELECT t.entity2, t.activity2
FROM table t;

答案 1 :(得分:2)

如果您有大量数据,则可能不想多次扫描表-union all就是这样做。

相反:

select (case when n.n = 1 then entity
             when n.n = 2 then entity_2
        end) as entity,
       (case when n.n = 1 then activity
             when n.n = 2 then activity_2
        end) as activity
from t cross join
     (select 1 as n from dual union all
      select 2 as n from dual
     ) n;

在Oracle 12C +中,使用横向联接可简化此操作:

select t.entity, s.activity
from t cross join lateral
     (select t.entity, t.activity from dual union all
      select t.entity_2, t.activity_2 from dual 
     ) s;

答案 2 :(得分:1)

select entity, activity from <table>
union all
select entity_2, activity_2 from <table>

答案 3 :(得分:0)

通常:

get_queryset