+ -
当前位置:首页 → 问答吧 → oracle竖表转横表

oracle竖表转横表

时间:2011-10-19

来源:互联网


student表中数据:

name type
--------------------------
大黄 3
小白 1
多多 1
大黄 1
大黄 2
小宝 1
小宝 2
小宝 3

查询结果:

type=1 type=2 type=3
------------------------
小白 null null
多多 null null
大黄 大黄 大黄
小宝 小宝 小宝


作者: yzncong   发布时间: 2011-10-19

SQL code
select max(case when type=1 then name end) t1,
    max(case when type=2 then name end) t2,
    max(case when type=3 then name end) t3
from student
group by name

作者: noteasytoregister   发布时间: 2011-10-19

“值过多”
什么意思?

作者: yzncong   发布时间: 2011-10-19

建议用decode

select t.name, 
max(decode(t.type,'1',t.type,null)) type1,
max(decode(t.type,'2',t.type,null)) type2,
max(decode(t.type,'3',t.type,null)) type3
from student t
group by t.name

作者: stu202060510   发布时间: 2011-10-19

晕,看错了。下面这个

select 
max(decode(t.type,'1',t.name,null)) type1,
max(decode(t.type,'2',t.name,null)) type2,
max(decode(t.type,'3',t.name,null)) type3
from test t
group by t.name

作者: stu202060510   发布时间: 2011-10-19

热门下载

更多