id name kecheng fenshu
1 张三 语文 89
1 张三 数学 90
1 张三 英语 99
2 王五 语文 80
2 王五 数学 99
2 王五 英语 88怎么转变
id name 语文 数学 英语
1 张三 89 90 99
2 王五 80 99 88
除了
select distinct t.id,t.name,
(select distinct t1.fenshu from emp3 t1 where t1.id=t.id and t1.kecheng='语文') 语文,
(select distinct t1.fenshu from emp3 t1 where t1.id=t.id and t1.kecheng='数学') 数学,
(select distinct t1.fenshu from emp3 t1 where t1.id=t.id and t1.kecheng='英语') 英语
from emp3 t
还有没有更简单的??
1 张三 语文 89
1 张三 数学 90
1 张三 英语 99
2 王五 语文 80
2 王五 数学 99
2 王五 英语 88怎么转变
id name 语文 数学 英语
1 张三 89 90 99
2 王五 80 99 88
除了
select distinct t.id,t.name,
(select distinct t1.fenshu from emp3 t1 where t1.id=t.id and t1.kecheng='语文') 语文,
(select distinct t1.fenshu from emp3 t1 where t1.id=t.id and t1.kecheng='数学') 数学,
(select distinct t1.fenshu from emp3 t1 where t1.id=t.id and t1.kecheng='英语') 英语
from emp3 t
还有没有更简单的??
max(decode(kecheng ,'语文',fenshu)) 语文,
max(decode(kecheng ,'数学',fenshu)) 数学,
max(decode(kecheng ,'英语',fenshu)) 英语
from emp3
grop by id,name;
select id,name,max(case when kecheng='语文' then fenshu end) 语文,
max(case when kecheng='数学' then fenshu end) 数学,
max(case when kecheng='英语' then fenshu end) 英语
from emp3
group by id,name
你可以看下oracle 的文档,里面写的很清楚,也很简单
http://www.oracle.com/technology/global/cn/pub/articles/oracle-database-11g-top-features/11g-pivot.html不过这个是要11g 才可用。
所以最好根据情况结合运用,个人建议而已~~