/*縱表轉橫表*/
create global temporary table test2
( uname varchar2(10),
subject varchar2(10),
score number
);insert into test2 values('王五','語文',80);
insert into test2 values('李四','語文',90);
insert into test2 values('李四','數學',80);
insert into test2 values('李四','英語',70);
insert into test2 values('張三','語文',80);
insert into test2 values('張三','數學',80);
insert into test2 values('張三','英語',90);
t.uname as 姓名,
sum(case t.subject when '語文' then t.score end)as 語文,
sum(case t.subject when '數學' then t.score end)as 數學,
sum(case t.subject when '英語' then t.score end)as 英語
from test2 t
group by t.uname;
/*橫表轉縱表 */
create global temporary table test4
( uname varchar2(10),
chinese number,
math number,
english number
);insert into test4 values('張三',80,90,70);
insert into test4 values('李四',90,85,95);
insert into test4 values('王五',88,75,90);
select t.uname,'平均分' as subject,cast((t.chinese+t.math+t.english)*1.0/3 as decimal(18,2)) as score from test4 t
union all
select t.uname,'總分' as subject,(t.chinese+t.math+t.english) as score from test4 t
)tborder by uname,
case tb.subject
when 'chinese' then 1
when 'math' then 2
when 'english' then 3
when '平均分' then 4
when '總分' then 5
新增上計算總分和平均分
sql語句最後加上case相當於排序。
oracle橫縱表相互轉換
縱表轉橫表 create global temporary table test2 uname varchar2 10 subject varchar2 10 score number insert into test2 values 王五 語文 80 insert into test2 value...
SQL server橫表縱表相互轉換
name course grade 張三yuwen 75張三 shuxue 80李四 yingyu 90李四 yuwen 95李四 shuxue 55name yuwen shuxue yingyu 張三75 8090 李四95550 縱表轉橫表 select name,sum case cours...
MySQL橫縱表相互轉化
先建立乙個成績表 縱表 create table user score name varchar 20 subjects varchar 20 score int insert into user score name,subjects,score values 張三 語文 60 insert in...