本文實例講述了Oracle實現(xiàn)行轉(zhuǎn)換成列的方法。分享給大家供大家參考,具體如下:
把行轉(zhuǎn)成列 把學生表,成績表,班級表,學科表 合并成一張成績表效果如下:
![](/d/20211018/93d80b233fc688d3d32f044ff88618bb.gif)
創(chuàng)建表
--班級表
create table CLASS
(
ID VARCHAR2(5) not null primary key,
CLASSNAME VARCHAR2(10)
);
--學生表
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)
);
--分數(shù)表
create table score(
sid varchar2(4),
subid varchar2(10),
score number(4,1)
);
查詢sql 如下
select s1.name 姓名,
s1.age 年齡,
s1.classname 班級,
score_.sid,
數(shù)學,
語文,
物理,
化學,
(數(shù)學 + 語文 + 物理 + 化學) 總分
from (select s.sid,
sum(decode(s.subid, 'SUB001', s.score)) 數(shù)學,
sum(decode(s.subid, 'SUB002', s.score)) 語文,
sum(decode(s.subid, 'SUB003', s.score)) 物理,
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.classid) s1 on s1.id = score_.sid
order by 總分;
更多關(guān)于Oracle相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Oracle常用函數(shù)匯總》、《Oracle日期與時間操作技巧總結(jié)》及《php+Oracle數(shù)據(jù)庫程序設計技巧總結(jié)》
希望本文所述對大家Oracle數(shù)據(jù)庫程序設計有所幫助。
您可能感興趣的文章:- Oracle實現(xiàn)行列轉(zhuǎn)換的方法分析
- Oracle的數(shù)據(jù)表中行轉(zhuǎn)列與列轉(zhuǎn)行的操作實例講解
- Oracle 數(shù)據(jù)庫針對表主鍵列并發(fā)導致行級鎖簡單演示
- oracle wm_concat 列轉(zhuǎn)行 逗號分隔
- Oracle逗號分隔列轉(zhuǎn)行實現(xiàn)方法
- 從Oracle 表格行列轉(zhuǎn)置說起
- Oracle CBO幾種基本的查詢轉(zhuǎn)換詳解
- oracle中to_date詳細用法示例(oracle日期格式轉(zhuǎn)換)
- 通過創(chuàng)建SQLServer 2005到 Oracle10g 的鏈接服務器實現(xiàn)異構(gòu)數(shù)據(jù)庫數(shù)據(jù)轉(zhuǎn)換方案
- MySQL轉(zhuǎn)換Oracle的需要注意的七個事項
- 將mysql轉(zhuǎn)換到oracle必須了解的50件事
- ORACLE常用數(shù)值函數(shù)、轉(zhuǎn)換函數(shù)、字符串函數(shù)