转摘pub:
如何实现表转置??
如何实现表转置??
SQL> select
2 max(decode(rn, 1, field2)) a,
3 max(decode(rn, 2, field2)) b,
4 max(decode(rn, 3, field2)) c
5 from(
7 select field1, field2, row_number() over(partition by field1 order by field2) rn
8 from t
9 )
10 group by field1
A B C---- ---- ----1 2 3
4 5 6
7 8 9
quote:
最初由 gufengge 发布
如题:
现在假如有表A:
field1 varchar2(4) not null
field2 varchar2(4) not null
表的内容如下:
field1 field2
a 1
a 2
a 3
b 4
b 5
b 6
c 7
c 8
c 9
希望得到表B内容如下:
a b c
1 2 3
4 5 6
7 8 9
请大家尽情发挥,答案越多越精彩,谢谢!!
标准行列转化
代码:
SQL> create table t (field1 varchar2(4), field2 varchar2(4));
表已创建。
SQL> insert into t values ('a', 1);
已创建 1 行。
SQL> insert into t values ('a', 2);
已创建 1 行。
SQL> insert into t values ('a', 3);
已创建 1 行。
SQL> insert into t values ('b', 4);
已创建 1 行。
SQL> insert into t values ('b', 5);
已创建 1 行。
SQL> insert into t values ('b', 6);
已创建 1 行。
SQL> insert into t values ('c', 7);
已创建 1 行。
SQL> insert into t values ('c', 8);
已创建 1 行。
SQL> insert into t values ('c', 9);
已创建 1 行。
SQL> commit;
提交完成。
SQL> select
2 max(decode(field1, 'a', field2)) a,
3 max(decode(field1, 'b', field2)) b,
4 max(decode(field1, 'c', field2)) c
5 from
6 (
7 select field1, field2, row_number() over(partition by field1 order by field2) rn
8 from t
9 )
10 group by rn;
A B C ---- ---- ---- 1 4 7
2 5 8
3 6 9
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9599/viewspace-472929/ ,如需转载,请注明出处,否则将追究法律责任。