±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
ʵ¼ÊÓ¦ÓÃÖÐÎÒÃÇ¿ÉÒÔͨ¹ýsum()ͳ¼Æ³ö×éÖеÄ×ܼƻòÕßÊÇÀÛ¼ÓÖµ£¬¾ßÌåʾÀýÈçÏ£º
1.´´½¨ÑÝʾ±í
create table emp
as
select * from scott.emp;
alter table emp
add constraint emp_pk
primary key(empno);
create table dept
as
select * from scott.dept;
alter table dept
add constraint dept_pk
primary key(deptno);
2. sum()Óï¾äÈçÏ£º
select deptno,
ename,
sal,
¡¡¡¡--°´ÕÕ²¿ÃÅнˮÀÛ¼Ó£¨order by¸Ä±äÁË·ÖÎöº¯ÊýµÄ×÷Óã¬Ö»¹¤×÷ÔÚµ±Ç°ÐкÍÇ°Ò»ÐУ¬¶ø²»ÊÇËùÓÐÐУ©
sum(sal) over (partition by deptno order by sal) ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
ʵ¼ÊÓ¦ÓÃÖÐÎÒÃÇ¿ÉÒÔͨ¹ýsum()ͳ¼Æ³ö×éÖеÄ×ܼƻòÕßÊÇÀÛ¼ÓÖµ£¬¾ßÌåʾÀýÈçÏ£º
1.´´½¨ÑÝʾ±í
create table emp
as
select * from scott.emp;
alter table emp
add constraint emp_pk
primary key(empno);
create table dept
as
select * from scott.dept;
alter table dept
add constraint dept_pk
primary key(deptno);
2. sum()Óï¾äÈçÏ£º
select deptno,
ename,
sal,
¡¡¡¡--°´ÕÕ²¿ÃÅнˮÀÛ¼Ó£¨order by¸Ä±äÁË·ÖÎöº¯ÊýµÄ×÷Óã¬Ö»¹¤×÷ÔÚµ±Ç°ÐкÍÇ°Ò»ÐУ¬¶ø²»ÊÇËùÓÐÐУ©
sum(sal) over (partition by deptno order by sal) ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
Èç¹ûÎÒÃÇ°´ÕÕʾÀýÏëµÃµ½Ã¿¸ö²¿ÃÅнˮֵ×î¸ßµÄ¹ÍÔ±µÄ¼Í¼£¬¿ÉÒÔÓÐËÄÖÖ·½·¨ÊµÏÖ£º
ÏÈ´´½¨Ê¾Àý±í
create table emp
as
select * from scott.emp;
alter table emp
add constraint emp_pk
primary key(empno);
create table dept
as
select * from scott.dept;
alter table dept
add constraint dept_pk
primary key(deptno);
·½·¨1.empÖеÄÿһÐж¼»á½øÐÐmax±È½Ï£¬·Ñʱ
select * from emp emp1 where emp1.sal=(select max(emp2.sal) from emp emp2 where emp2.deptno=emp1.deptno)
·½·¨2.ÏÈ×Ó²éѯ²éÕÒ³ömax sal£¬È»ºóÓëemp±íÏà¹ØÁª£¬Èç¹ûÂß¼¸´ÔÓ»á²úÉú½Ï¶à´úÂë
select * from emp emp1,(select de ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
Èç¹ûÎÒÃÇ°´ÕÕʾÀýÏëµÃµ½Ã¿¸ö²¿ÃÅнˮֵ×î¸ßµÄ¹ÍÔ±µÄ¼Í¼£¬¿ÉÒÔÓÐËÄÖÖ·½·¨ÊµÏÖ£º
ÏÈ´´½¨Ê¾Àý±í
create table emp
as
select * from scott.emp;
alter table emp
add constraint emp_pk
primary key(empno);
create table dept
as
select * from scott.dept;
alter table dept
add constraint dept_pk
primary key(deptno);
·½·¨1.empÖеÄÿһÐж¼»á½øÐÐmax±È½Ï£¬·Ñʱ
select * from emp emp1 where emp1.sal=(select max(emp2.sal) from emp emp2 where emp2.deptno=emp1.deptno)
·½·¨2.ÏÈ×Ó²éѯ²éÕÒ³ömax sal£¬È»ºóÓëemp±íÏà¹ØÁª£¬Èç¹ûÂß¼¸´ÔÓ»á²úÉú½Ï¶à´úÂë
select * from emp emp1,(select de ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
ÓÐЩʱºòÎÒÃÇÏ£ÍûµÃµ½Ö¸¶¨Êý¾ÝÖеÄÇ°nÁУ¬Ê¾ÀýÈçÏ£º
µÃµ½Ã¿¸ö²¿ÃÅнˮ×î¸ßµÄÈý¸ö¹ÍÔ±£º
ÏÈ´´½¨Ê¾Àý±í
create table emp
as
select * from scott.emp;
alter table emp
add constraint emp_pk
primary key(empno);
create table dept
as
select * from scott.dept;
alter table dept
add constraint dept_pk
primary key(deptno);
ÏÈ¿´Ò»ÏÂrow_number() /rank()/dense_rank()Èý¸öº¯ÊýÖ®¼äµÄÇø±ð
select emp.deptno,emp.sal,emp.empno,row_number() over (partition by deptno order by sal desc) row_number, --1,2,3
rank() over (partition by deptno order by sal desc)  ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
ÓÐЩʱºòÎÒÃÇÏ£ÍûµÃµ½Ö¸¶¨Êý¾ÝÖеÄÇ°nÁУ¬Ê¾ÀýÈçÏ£º
µÃµ½Ã¿¸ö²¿ÃÅнˮ×î¸ßµÄÈý¸ö¹ÍÔ±£º
ÏÈ´´½¨Ê¾Àý±í
create table emp
as
select * from scott.emp;
alter table emp
add constraint emp_pk
primary key(empno);
create table dept
as
select * from scott.dept;
alter table dept
add constraint dept_pk
primary key(deptno);
ÏÈ¿´Ò»ÏÂrow_number() /rank()/dense_rank()Èý¸öº¯ÊýÖ®¼äµÄÇø±ð
select emp.deptno,emp.sal,emp.empno,row_number() over (partition by deptno order by sal desc) row_number, --1,2,3
rank() over (partition by deptno order by sal desc)  ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
ÓÐʱºò±¨±íÉÏÃæÐèÒªÏÔʾ¸Ã±Ê²Ù×÷µÄÉÏÒ»²½Öè»òÕßÏÂÒ»²½ÖèµÄÏêϸÐÅÏ¢£¬Õâ¸öʱºò¿ÉÒÔ°´ÕÕÏÂÃæµÄ×ö·¨£º
ÏÈ´´½¨Ê¾Àý±í£º
-- Create table
create table LEAD_TABLE
(
CASEID VARCHAR2(10),
STEPID VARCHAR2(10),
ACTIONDATE DATE
)
tablespace COLM_DATA
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
insert into LEAD_TABLE values('Case1','Step1',to_date('20070101','yyyy-mm-dd'));
insert into LEAD_TABLE&n ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Ò»)--sum()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(¶þ)--max()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(Èý)--row_number() /rank()/dense_rank()
[Oracle]¸ßЧµÄSQLÓï¾äÖ®·ÖÎöº¯Êý(ËÄ)--lag()/lead()
ÓÐʱºò±¨±íÉÏÃæÐèÒªÏÔʾ¸Ã±Ê²Ù×÷µÄÉÏÒ»²½Öè»òÕßÏÂÒ»²½ÖèµÄÏêϸÐÅÏ¢£¬Õâ¸öʱºò¿ÉÒÔ°´ÕÕÏÂÃæµÄ×ö·¨£º
ÏÈ´´½¨Ê¾Àý±í£º
-- Create table
create table LEAD_TABLE
(
CASEID VARCHAR2(10),
STEPID VARCHAR2(10),
ACTIONDATE DATE
)
tablespace COLM_DATA
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
insert into LEAD_TABLE values('Case1','Step1',to_date('20070101','yyyy-mm-dd'));
insert into LEAD_TABLE&n ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
--1. È¡Ç°10ÐÐ
select * from hr.employees where rownum<=10
--2. °´ÕÕfirst_nameÉýÐò£¬È¡Ç°10λ
--ÕýÈ··½·¨ oracle´¦Àí»úÖÆ: --> hr.employeesÈ«±íɨÃè
--> SORT ORDER BY STOPKEY Ö»ÅÅÐòÇ°10ÐУ¬×÷Ϊһ¸ö¾ØÕó½á¹¹
-->ʣϵÄÐÐÓëµÚ10ÐнøÐбȽϣ¬ºÏÊʵĽøÈë¾ØÕó,·ñÔòÅ×Æú
--Óŵ㣺RAMÖÐÉÙÁ¿ÅÅÐò£¬Ëٶȿì(²»ÐèÒªÔÚÄÚ´ ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
--1. È¡Ç°10ÐÐ
select * from hr.employees where rownum<=10
--2. °´ÕÕfirst_nameÉýÐò£¬È¡Ç°10λ
--ÕýÈ··½·¨ oracle´¦Àí»úÖÆ: --> hr.employeesÈ«±íɨÃè
--> SORT ORDER BY STOPKEY Ö»ÅÅÐòÇ°10ÐУ¬×÷Ϊһ¸ö¾ØÕó½á¹¹
-->ʣϵÄÐÐÓëµÚ10ÐнøÐбȽϣ¬ºÏÊʵĽøÈë¾ØÕó,·ñÔòÅ×Æú
--Óŵ㣺RAMÖÐÉÙÁ¿ÅÅÐò£¬Ëٶȿì(²»ÐèÒªÔÚÄÚ´ ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
±êÁ¿×Ó²éѯ
ORACLEÔÊÐíÔÚselect×Ó¾äÖаüº¬µ¥ÐÐ×Ó²éѯ, ʹÓñêÁ¿×Ó²éѯ¿ÉÒÔÓÐЧµÄ¸ÄÉÆÐÔÄÜ£¬µ±Ê¹Óõ½ÍⲿÁ¬½Ó£¬»òÕßʹÓõ½Á˾ۺϺ¯Êý£¬¾Í¿ÉÒÔ¿¼ÂDZêÁ¿×Ó²éѯµÄ¿ÉÄÜÐÔ
1. È¡ÏûÍⲿÁ¬½ÓµÄʹÓÃ
ÍⲿÁ¬½ÓµÄ×ö·¨£º
select a.username,count(*) from all_users a,all_objects b
where a.username=b.owner(+)
group by a.username;
¸Ä³É±êÁ¿×Ó²éѯµÄ×ö·¨£º
select a.username,(select count(*) from all_objects b where b.owner=a.username) cnt
from all_users a;
PS: Á½ÖÖ×ö·¨µÃµ½µÄ½á¹û»áÓÐЩÐí²î±ð£¬Ö÷ÒªÔÚall_objectsûÓзûºÏÌõ¼þµÄÐÐʱ, ÍⲿÁ¬½ÓµÄcount(*)=1£¬¶ø±êÁ¿×Ó²éѯµÄcount(*)½á¹û=0
select a.u ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
±êÁ¿×Ó²éѯ
ORACLEÔÊÐíÔÚselect×Ó¾äÖаüº¬µ¥ÐÐ×Ó²éѯ, ʹÓñêÁ¿×Ó²éѯ¿ÉÒÔÓÐЧµÄ¸ÄÉÆÐÔÄÜ£¬µ±Ê¹Óõ½ÍⲿÁ¬½Ó£¬»òÕßʹÓõ½Á˾ۺϺ¯Êý£¬¾Í¿ÉÒÔ¿¼ÂDZêÁ¿×Ó²éѯµÄ¿ÉÄÜÐÔ
1. È¡ÏûÍⲿÁ¬½ÓµÄʹÓÃ
ÍⲿÁ¬½ÓµÄ×ö·¨£º
select a.username,count(*) from all_users a,all_objects b
where a.username=b.owner(+)
group by a.username;
¸Ä³É±êÁ¿×Ó²éѯµÄ×ö·¨£º
select a.username,(select count(*) from all_objects b where b.owner=a.username) cnt
from all_users a;
PS: Á½ÖÖ×ö·¨µÃµ½µÄ½á¹û»áÓÐЩÐí²î±ð£¬Ö÷ÒªÔÚall_objectsûÓзûºÏÌõ¼þµÄÐÐʱ, ÍⲿÁ¬½ÓµÄcount(*)=1£¬¶ø±êÁ¿×Ó²éѯµÄcount(*)½á¹û=0
select a.u ......