列转行的问题
行转列的问题见的比较多,列转行怎么做;
比如现在有表定义如下
Table t1
f1 f2 f3
A 1 2
B 3 4
想得到下面的结果
A f2 1
A f3 2
B f2 3
B f3 4
------解决方案--------------------
with t1 as
(
select 'A' f1, 1 f2, 2 f3 from dual
union all
select 'B' f1, 2 f2, 4 f3 from dual
)
select t1.f1,
case when t2.f1 = 'F2' then t1.f2
when t2.f1 = 'F3' then t1.f3 else null end
from t1,
(select 'F2' f1 from dual
union all
select 'F3' f1 from dual) t2
------解决方案--------------------
with t1 as
(
select 'A' f1, 1 f2, 2 f3 from dual
union all
select 'B' f1, 2 f2, 4 f3 from dual
)
select t1.f1,t2.f1 f3,
case when t2.f1 = 'F2' then t1.f2
when t2.f1 = 'F3' then t1.f3 else null end
from t1,
(select 'F2' f1 from dual
union all
select 'F3' f1 from dual) t2
------解决方案--------------------
select f1, 'f2', f2
from t1
union all
select f1, 'f3', f3
from t1
--order by ...
按什么排序,自已加上就行