Oracle学习之旅——行转列实例分析


with t as
(SELECT &a a FROM dual)SELECT * FROM t
输入的格式这样&a=''fdf,gym,huijh,ggjhj'
输出:

 Oracle学习之旅——行转列实例分析_第1张图片     

分析过程:

    这是是一个典型的字符串的行转列题目。

    需要通过:字符串拆分——循环查询的思路来做

    知识储备:

             一、函数Substr、Instr、Decode、Connect by

             二、连接字符‘||’

with t 
as 
(SELECT &a a FROM dual)
select substr(&a||',',decode(rownum,1,1,instr(&a||',',',',1,rownum-1)+1),instr(&a||',',',',1,rownum)-decode(rownum,1,1,instr(&a||',',',',1,rownum-1)+1)) 字符
 from t
connect by instr(&a||',', ',', 1, rownum)>0
    


题目二:

 create table tab(time date,num int,city varchar(20));

insert into tab values(to_date('2013-7-1','yyyy-mm-dd'),100,'北京');
insert into tab values(to_date('2013-7-2','yyyy-mm-dd'),1000,'北京');
insert into tab values(to_date('2013-7-3','yyyy-mm-dd'),10000,'北京');
insert into tab values(to_date('2013-7-1','yyyy-mm-dd'),500,'上海');
insert into tab values(to_date('2013-7-2','yyyy-mm-dd'),5000,'上海');
insert into tab values(to_date('2013-7-3','yyyy-mm-dd'),50000,'上海');
select *  from tab;
输入后得:
    TIME NUM CITY
1 2013/7/1 100 北京
2 2013/7/2 1000 北京
3 2013/7/3 10000 北京
4 2013/7/1 500 上海
5 2013/7/2 5000 上海
6 2013/7/3 50000 上海
输出:


select TAB.CITY,listagg(to_char(TAB.TIME,'YYYY-MM-DD')||' '||TAB.NUM,' ') WITHIN GROUP(ORDER BY TAB.CITY) A
from tab
GROUP BY TAB.CITY

Oracle学习之旅——行转列实例分析_第2张图片


题目三:

输入:

create table lession(lession_id number, lession_name varchar2(8));

insert into LESSION (lession_id, lession_name) values (1, '语文');
insert into LESSION (lession_id, lession_name) values (2, '数学');
insert into LESSION (lession_id, lession_name) values (3, '英语');
insert into LESSION (lession_id, lession_name) values (4, '物理');
insert into LESSION (lession_id, lession_name) values (5, '化学');

create table score(stu_id number,grade number(4),lession_id number(4));

insert into SCORE (stu_id, grade, lession_id) values (1001, 98, 1);
insert into SCORE (stu_id, grade, lession_id) values (1002, 88, 1);
insert into SCORE (stu_id, grade, lession_id) values (1005, 79, 1);
insert into SCORE (stu_id, grade, lession_id) values (1004, 89, 5);
insert into SCORE (stu_id, grade, lession_id) values (1005, 92, 4);
insert into SCORE (stu_id, grade, lession_id) values (1001, 57, 4);
insert into SCORE (stu_id, grade, lession_id) values (1005, 97, 2);
insert into SCORE (stu_id, grade, lession_id) values (1001, 98, 2);
insert into SCORE (stu_id, grade, lession_id) values (1002, 78, 3);
insert into SCORE (stu_id, grade, lession_id) values (1003, 94, 1);
insert into SCORE (stu_id, grade, lession_id) values (1004, 89, 2);
insert into SCORE (stu_id, grade, lession_id) values (1005, 92, 3);
insert into SCORE (stu_id, grade, lession_id) values (1006, 94, 2);
insert into SCORE (stu_id, grade, lession_id) values (1004, 87, 1);
insert into SCORE (stu_id, grade, lession_id) values (1006, 94, 1);
insert into SCORE (stu_id, grade, lession_id) values (1002, 78, 2);
insert into SCORE (stu_id, grade, lession_id) values (1003, 90, 4);
insert into SCORE (stu_id, grade, lession_id) values (1003, 80, 2);
insert into SCORE (stu_id, grade, lession_id) values (1001, 88, 3);
insert into SCORE (stu_id, grade, lession_id) values (1003, 94, 3);
insert into SCORE (stu_id, grade, lession_id) values (1004, 60, 3);
insert into SCORE (stu_id, grade, lession_id) values (1006, 74, 3);
insert into SCORE (stu_id, grade, lession_id) values (1002, 89, 4);
insert into SCORE (stu_id, grade, lession_id) values (1004, 94, 4);
insert into SCORE (stu_id, grade, lession_id) values (1002, 67, 5);
insert into SCORE (stu_id, grade, lession_id) values (1001, 79, 5);
insert into SCORE (stu_id, grade, lession_id) values (1003, 93, 5);
insert into SCORE (stu_id, grade, lession_id) values (1005, 89, 5);
insert into SCORE (stu_id, grade, lession_id) values (1006, 90, 5);
insert into SCORE (stu_id, grade, lession_id) values (1006, 95, 4);

create table student(stu_id number,stu_name varchar2(8),stu_age number(4),class_id number(4));

insert into STUDENT (stu_id, stu_name, stu_age, class_id) values (1001, '张三', 23, 1);
insert into STUDENT (stu_id, stu_name, stu_age, class_id) values (1002, '李四', 19, 1);
insert into STUDENT (stu_id, stu_name, stu_age, class_id) values (1003, '王五', 22, 1);
insert into STUDENT (stu_id, stu_name, stu_age, class_id) values (1004, '赵六', 25, 2);
insert into STUDENT (stu_id, stu_name, stu_age, class_id) values (1005, '小李', 24, 2);
insert into STUDENT (stu_id, stu_name, stu_age, class_id) values (1006, '小明', 22, 2);



分析过程:

    这是是多行转多列的题目。

    需要通过:字符串拆分——循环查询的思路来做

    知识储备:

             函数rank、decode、wmsys.wm_concat、group by


with t 
as 
(select l.lession_id,s.stu_name,l.lession_name,c.grade,
rank() over (partition by l.lession_name order by c.grade desc) RA,
stu_name||','||to_char(grade) tog
from score c,student s,lession l
where c.stu_id=s.stu_id
and c.lession_id=l.lession_id
order by c.lession_id,c.grade desc)
--select * from t
select 科目,max(第一名) 第一名,max(第二名) 第二名,max(第三名) 第三名
from
(select lession_name 科目,
decode(RA,'1',to_char(wmsys.wm_concat(tog)))  第一名,
decode(RA,'2',to_char(wmsys.wm_concat(tog)))  第二名,
decode(RA,'3',to_char(wmsys.wm_concat(tog)))  第三名
from t
group by lession_name,RA)
group by 科目
order by 科目 desc
结果

Oracle学习之旅——行转列实例分析_第3张图片

             

你可能感兴趣的:(oracle)