±¾ÏµÁÐÎÄÕµ¼º½
[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 ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
ʹÓÃPackageµÄÓŵãÔÚÓÚÌṩÁ˱ØÐèµÄ³ÌÐòÉè¼Æ½á¹¹, ´Ù½øÁËÄ£¿é»¯±à³ÌÉè¼Æ, ×îÖØÒªµÄÊÇPackage¶Ï¿ªÁËÒÀÀµÁ´, ʹµÃ¶Ôij¸öÊý¾Ý¿âģʽµÄ¸Ä¶¯²»»áµ¼ÖÂÕû¸öģʽµÄÎÞЧ,´Ó¶ø±ÜÃâÁË°º¹óµÄÖرàÒë!
ÀýÈç: ´æÔÚtable t , procedure p1 p2, view v, function f, ËüÃÇÖ®¼äÒýÓùØϵÈçÏ£º
select name,type,referenced_name,referenced_type from user_dependencies
where referenced_owner='SCOTT'
order by name;
F FUNCTION T TABLE
P1 PROCEDURE &nbs ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
ʹÓÃPackageµÄÓŵãÔÚÓÚÌṩÁ˱ØÐèµÄ³ÌÐòÉè¼Æ½á¹¹, ´Ù½øÁËÄ£¿é»¯±à³ÌÉè¼Æ, ×îÖØÒªµÄÊÇPackage¶Ï¿ªÁËÒÀÀµÁ´, ʹµÃ¶Ôij¸öÊý¾Ý¿âģʽµÄ¸Ä¶¯²»»áµ¼ÖÂÕû¸öģʽµÄÎÞЧ,´Ó¶ø±ÜÃâÁË°º¹óµÄÖرàÒë!
ÀýÈç: ´æÔÚtable t , procedure p1 p2, view v, function f, ËüÃÇÖ®¼äÒýÓùØϵÈçÏ£º
select name,type,referenced_name,referenced_type from user_dependencies
where referenced_owner='SCOTT'
order by name;
F FUNCTION T TABLE
P1 PROCEDURE &nbs ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
ÅúÁ¿´¦ÀíÒ»°ãÓÃÔÚETL²Ù×÷, ETL´ú±íÌáÈ¡(extract),ת»»(transform),×°ÔØ(load), ÊÇÒ»¸öÊý¾Ý²Ö¿âµÄ´Ê»ã!
ÀàËÆÓÚÏÂÃæµÄ½á¹¹£º
for x (select * from...)
loop
Process data;
insert into table values(...);
end loop;
Ò»°ãÇé¿öÏÂ, ÎÒÃÇ´¦Àí´ó±ÊµÄÊý¾Ý²åÈ붯×÷, ÓÐ2ÖÖ×ö·¨, µÚÒ»ÖÖ¾ÍÊÇÒ»±Ê±ÊµÄÑ»·²åÈë
create table t1 as select * from user_tables where 1=0;
create table t2 as select * from user_tables where 1=0;
create table t3 as select table_name from user_tables where& ......
±¾ÏµÁÐÎÄÕµ¼º½
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Ò»)--αÁÐROWNUMʹÓü¼ÇÉ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(¶þ)--±êÁ¿×Ó²éѯ
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Èý)--PackageµÄÓŵã
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(ËÄ)--ÅúÁ¿´¦Àí
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Îå)--µ÷Óô洢¹ý³Ì·µ»Ø½á¹û¼¯
[Oracle]¸ßЧµÄPL/SQL³ÌÐòÉè¼Æ(Áù)--%ROWTYPEµÄʹÓÃ
ÅúÁ¿´¦ÀíÒ»°ãÓÃÔÚETL²Ù×÷, ETL´ú±íÌáÈ¡(extract),ת»»(transform),×°ÔØ(load), ÊÇÒ»¸öÊý¾Ý²Ö¿âµÄ´Ê»ã!
ÀàËÆÓÚÏÂÃæµÄ½á¹¹£º
for x (select * from...)
loop
Process data;
insert into table values(...);
end loop;
Ò»°ãÇé¿öÏÂ, ÎÒÃÇ´¦Àí´ó±ÊµÄÊý¾Ý²åÈ붯×÷, ÓÐ2ÖÖ×ö·¨, µÚÒ»ÖÖ¾ÍÊÇÒ»±Ê±ÊµÄÑ»·²åÈë
create table t1 as select * from user_tables where 1=0;
create table t2 as select * from user_tables where 1=0;
create table t3 as select table_name from user_tables where& ......