Oracle實現行轉換成列的方法

2022-09-22 00:09:18 字數 1221 閱讀 8508

把行轉成列 把學生表,成績表,班級表,學科表 合併成一張成績表效果如下:

建立表--班級表

create table class

( id varchar2(5) not null primary key,

classname varchar2(10)

);--學生www.cppcns.com表

create table student

( id varchar2(10) not null primary key,

name varchar2(10),

age number(3),

classid varchar2(5)

);--科目表

create table subject(

id varchar2(10) primary key,

subname varchar2(10)

);--分數表

create table score(

sid varchar2(4),

subid varchar2(10),

score number(4,1)

);查詢sql 如下

select s1.name 姓名,

s1.age 年齡,

s1.classname 班級,

score_.sid,

數學,語文,

物理,化學,

(數學 + 語文 + 物理 + 化學) 總分

from (select s.sid,

sum(decode(s.subid, 'sub001', s.score)) 數學,

sum(decode(s.subid, 'sub002', s.score)) 語文,

sum(decode(s.subid, 'sub003', s.score)) 物理,

lzmjsny www.cppcns.com sum(decode(s.subid,'sub004',s.score)) 化學

from score s

group by s.sid) score_

right join (select st.id, st.name, st.age, c.classname

from student st, class c

where c.id = st.cllzmjsnyassid) s1 on s1.id = score_.sid

order by 總分;

行索引轉換成列

reset index 函式原型 dataframe.reset index level none,drop false,inplace false,col level 0,col fill 引數解釋 level int str tuple或list,預設無,僅從索引中刪除給定級別。預設情況下移除所...

oracle實現行列轉換

ql select from student idname chinese math english 1a 90 70 80 2b 80 70 90 3c 80 90 70 select id,name,chinese 課程,chinese 分數 from student union all sel...

Oracle轉換成SqlServer資料庫的步驟

1 背景 itm系統目前支援mysql oracle資料庫,由於現在需要和ca產品進行結合,用ca產品的採集功能進行資料採集,因此需要使用ca產品的sqlserver資料庫,為了使得系統支援sqlserver資料庫,首先需將oracle資料庫遷移到mysql中,然後修改系統中dao對應xml配置檔案...