--oracle的行列转换
--先建立一个销售表
create?table?销售(商品名称 varchar2(10),季度 char(2),销售额 number);
insert?into?销售 values('电视机','01',100);
insert?into?销售 values('电视机','02',200);
insert?into?销售 values('电视机','03',300);
insert?into?销售 values('空调','01',50);
insert?into?销售 values('空调','02',150);
insert?into?销售 values('空调','03',180);
--查询一下销售表
select?*?from?销售;
--行转列(使用sum、decode、group by函数,as 可以省略)
select?商品名称,
sum(decode(a.季度,01,a.销售额,0))?as?一季度,
sum(decode(a.季度,'02',a.销售额,0))?as?二季度,
sum(decode(a.季度,'03',a.销售额,0))?as?三季度,
sum(decode(a.季度,'04',a.销售额,0))?as?四季度
from?销售 a group?by?a.商品名称;
--ROWNUM使用
--先创建一个yggz表
create?table?yggz(bh number(6)?,gz number);
insert?into?yggz values(1,1000);
insert?into?yggz values(2,1100);
insert?into?yggz values(3,900);
insert?into?yggz values(4,2000);
insert?into?yggz values(5,1500);
insert?into?yggz values(6,3000);
insert?into?yggz values(7,1400);
insert?into?yggz values(8,1200);
commit;
--ROWNUM(作用:对查询结果,输出若干条记录 注意:只能与<、<=、between and连用)
--分别尝试获得rownum<3,rownum=3,rownum>3,rownum<=3的查询结果。
select?*?from?yggz where?rownum<3;--前两条
select?*?from?yggz where?rownum=3;--空
select?*?from?yggz where?rownum>3;--空
select?*?from?yggz where?rownum<=3;--前三条
--取从1到3
select?*?from?yggz where?rownum?between?1?and?3;--2 between 3是取不到的。
--任务:查找表中,第3条到第5条记录,并显示出来。
select?*?from?yggz where?rownum?<6;
--使用minus集合相减
select?*?from?yggz where?rownum<=5
minus
select?*?from?yggz where?rownum<=2;
--使用嵌套(rownum不能<>同时使用,给rownum取个别名)
select?yggz.*,?rownum?from?yggz;
select?*?from?(select?yggz.*,?rownum?cnm from?yggz)?where?cnm<=5?and?cnm>=3;