+ -
当前位置:首页 → 问答吧 → 求一条行转列的sql语句

求一条行转列的sql语句

时间:2011-12-01

来源:互联网

遇到一个问题,来请教大家,我用oracle联表查询做统计,出来的结果是这样的
code state count(state)
3310 on 13
3310 off 2
我现在想把这个结果转换成一列,像下面这样
code on off
3310 13 2
不知道有没有办法做到,知道的大哥帮忙说下,万分感谢啊

作者: jkil216633   发布时间: 2011-12-01

SQL code
太多例子了!

max+case when

作者: cosio   发布时间: 2011-12-01

SQL code
select code,
max(case when state ='on' then count else 0 end) on,
max(case when state ='off' then count else 0 end) off
fromgroup by code

select code,
       max(decode(state,'on',count,0)) on,
       max(decode(state,'off',count,0)) off
fromgroup by code

作者: cosio   发布时间: 2011-12-01