SQL server橫表縱表相互轉換

2022-09-11 00:18:45 字數 836 閱讀 9602

name 

course

grade

張三yuwen

75張三

shuxue

80李四

yingyu

90李四

yuwen

95李四

shuxue

55name

yuwen

shuxue

yingyu

張三75

8090

李四95550

縱表轉橫表

select name,

sum(case course when 'yuwen' then grade else 0 end) as 'yuwen',

sum(case course when 'shuxue' then grade else 0 end) as 'shuxue',

sum(case course when 'yingyu' then grade else 0 end) as 'yingyu'

from 縱表

group by name

橫表轉縱表

select name,'yuwen' as 科目,yuwen as 成績 from 橫表 union all

select name,'shuxue' as 科目,shuxue as 成績 from 橫表 union all

select name,'yingyu' as 科目,yingyu as 成績 from 橫表

order by name,科目 desc;

每天學習一點點。

oracle橫縱表相互轉換

縱表轉橫表 create global temporary table test2 uname varchar2 10 subject varchar2 10 score number insert into test2 values 王五 語文 80 insert into test2 value...

MySQL橫縱表相互轉化

先建立乙個成績表 縱表 create table user score name varchar 20 subjects varchar 20 score int insert into user score name,subjects,score values 張三 語文 60 insert in...

oracle橫縱表相互轉換

縱表轉橫表 create global temporary table test2 uname varchar2 10 subject varchar2 10 score number insert into test2 values 王五 語文 80 insert into test2 value...