日期:2014-05-16  浏览次数:20388 次

Oracle特殊语句

(case? when instr(sample_num,'R')>0 then 1 else 0 end) R_flag

?

分析函数,行列转换【原博客链接:http://blog.csdn.net/herohuaxu/article/details/5816111】

Create Table temp
(
num varchar2(15 Char),
name varchar2(20 Char),
sex varchar2(2 Char),
classes varchar2(30 Char),
course_name varchar2(50 Char)
);

?

------------------------------------------------------------------------------------------------------------------------

insert into temp(num,name,sex,classes,course_name)? values ('206211','王艺','男','06-1班','保险学');
insert into temp(num,name,sex,classes,course_name) values ('206212','肖薇','女','06-2','保险学');
insert into temp(num,name,sex,classes,course_name) values ('206212','肖薇','女','06-2','财务管理');
insert into temp(num,name,sex,classes,course_name) values ('206212','肖薇','女','06-2','财务会计');
insert into temp(num,name,sex,classes,course_name) values ('206213','陈雅诗','女','06-2','电子商务');
insert into temp(num,name,sex,classes,course_name) values ('206213','陈雅诗','女','06-2','公共经济学');
insert into temp(num,name,sex,classes,course_name) values ('206213','陈雅诗','女','06-2','公司理财');
insert into temp(num,name,sex,classes,course_name) values ('206213','陈雅诗','女','06-2','管理学原理');
insert into temp(num,name,sex,classes,course_name) values ('206213','陈雅诗','女','06-2','保险学');
insert into temp(num,name,sex,classes,course_name) values ('206214','李丹阳','男','06-1','保险学');
insert into temp(num,name,sex,classes,course_name) values ('206214','李丹阳','男','06-1','财务管理');
insert into temp(num,name,sex,classes,course_name) values ('206214','李丹阳','男','06-1','财务会计');
insert into temp(num,name,sex,classes,course_name) values ('206214','李丹阳','男','06-1','电子商务');
insert into temp(num,name,sex,classes,course_name) values ('206214','李丹阳','男','06-1','公共经济学');
insert into temp(num,name,sex,classes,course_name) values ('206215','杨伊琳','女','06-3班','环境管理学');
insert into temp(num,name,sex,classes,course_name) values ('206215','杨伊琳','女','06-3班','管理学原理');
insert into temp(num,name,sex,classes,course_name) values ('206215','杨伊琳','女','06-3班','商务谈判');
insert into temp(num,name,sex,classes,course_name) values ('206216','李佳琪','男','06-2','土地估计');

------------------------------------------------------------------------------------------------------------------------

select max(count(course_name))
from?? temp
group? by num,name,sex,classes;

------------------------------------------------------------------------------------------------------------------------

select num,name,sex,classes,course_name,
row_number() over(partition by num,name,sex,classes order by course_name) rn
from?? temp;

------------------------------------------------------------------------------------------------------------------------

select num,name,sex,classes,
??????????? max(decode(rn,1,course_name,null)) course_name_1,
??????????? max(decode(rn,2,course_name,null)) course_name_2,
??????????? max(decode(rn,3,course_name,null)) course_name_3,
??????????? max(decode(rn,4,course_name,null)) course_name_4,
??????????? max(decode(rn,5,course_name,null)) course_name_5
from?? (select num,name,sex,classes,course_name,
row_number() over(partition by num,name,sex,classes order by course_name) rn
?????????????????? from?? temp)
group? by num,name,sex,classes;

------------------------------------------------------------------------------------------------------------------------

select num,name,sex,classes,
? (max(decode(rn,1,course_name,null)) || max(decode(rn,2,',' || course_name,null)) || max(decode(rn,3,',' || course_name,null)) || max(decode(rn,4,',' || course_name,null)) ||
??????????? max(decode(rn,5,',' || course_name,null))) name
from?? (select num,name,sex,classes,course_name,
?????????????????? row_number() over(partition by n