row_number() OVER (PARTITION BY COL1 ORDER BY COL2) ±íʾ¸ù¾ÝCOL1·Ö×飬ÔÚ·Ö×éÄÚ²¿¸ù¾Ý COL2ÅÅÐò£¬¶ø´Ëº¯Êý¼ÆËãµÄÖµ¾Í±íʾÿ×éÄÚ²¿ÅÅÐòºóµÄ˳Ðò±àºÅ£¨×éÄÚÁ¬ÐøµÄΨһµÄ).
ÓërownumµÄÇø±ðÔÚÓÚ£ºÊ¹ÓÃrownum½øÐÐÅÅÐòµÄʱºòÊÇÏȶԽá¹û¼¯¼ÓÈëαÁÐrownumÈ»ºóÔÙ½øÐÐÅÅÐò£¬¶ø´Ëº¯ÊýÔÚ°üº¬ÅÅÐò´Ó¾äºóÊÇÏÈÅÅÐòÔÙ¼ÆËãÐкÅÂ룮
row_number()ºÍrownum²î²»¶à£¬¹¦Äܸüǿһµã£¨¿ÉÒÔÔÚ¸÷¸ö·Ö×éÄÚ´Ó1¿ªÊ±ÅÅÐò£©£®
rank()ÊÇÌøÔ¾ÅÅÐò£¬ÓÐÁ½¸öµÚ¶þÃûʱ½ÓÏÂÀ´¾ÍÊǵÚËÄÃû£¨Í¬ÑùÊÇÔÚ¸÷¸ö·Ö×éÄÚ£©£®
dense_rank()lÊÇÁ¬ÐøÅÅÐò£¬ÓÐÁ½¸öµÚ¶þÃûʱÈÔÈ»¸ú×ŵÚÈýÃû¡£Ïà±ÈÖ®ÏÂrow_numberÊÇûÓÐÖØ¸´ÖµµÄ £®
lag£¨arg1,arg2,arg3):
arg1ÊÇ´ÓÆäËûÐзµ»ØµÄ±í´ïʽ
arg2ÊÇÏ£Íû¼ìË÷µÄµ±Ç°ÐзÖÇøµÄÆ«ÒÆÁ¿¡£ÊÇÒ»¸öÕýµÄÆ«ÒÆÁ¿£¬Ê±Ò»¸öÍù»Ø¼ìË÷ÒÔǰµÄÐеÄÊýÄ¿¡£
arg3ÊÇÔÚarg2±íʾµÄÊýÄ¿³¬³öÁË·Ö×éµÄ·¶Î§Ê±·µ»ØµÄÖµ¡£
¿´¼¸¸öSQLÓï¾ä:
Óï¾äÒ»:
select row_number() over(order by sale/cnt desc) as sort, sale/cnt
from (
select -60 as sale,3 as cnt from dual union
select 24 as sale,6 as cnt from dual union
select 50 as sale,5 as cnt from dual unio ......
Oracle µÄrownum ÔÀíºÍʹÓÃ
ÔÚOracle ÖУ¬Òª°´Ìض¨Ìõ¼þ²éѯǰNÌõ¼Ç¼£¬Óøörownum ¾Í¸ã¶¨ÁË¡£
select * from emp where rownum <= 5
¶øÇÒÊéÉÏÒ²¸æ½ë£¬²»ÄܶÔrownum ÓÃ">"£¬ÕâÒ²¾ÍÒâζ×Å£¬Èç¹ûÄãÏëÓÃ
select * from emp where rownum > 5
ÔòÊÇʧ°ÜµÄ¡£ÒªÖªµÀΪʲô»áʧ°Ü£¬ÔòÐèÒªÁ˽ârownum ±³ºóµÄ»úÖÆ£º
1 Oracle executes your query.
2 Oracle fetches the first row and calls it row number 1.
3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.
4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).
5 Go to step 3.
Á˽âÁËÔÀí£¬¾ÍÖªµÀrownum >²»»á³É¹¦£¬ÒòΪÔÚµÚÈý²½µÄʱºò²éѯ³öµÄÐÐÒѾ±»¶ªÆú£¬µÚËIJ½²é³öÀ´µÄrownum ÈÔÈ»ÊÇ1,ÕâÑùÓÀÔ¶Ò²²»»á³É¹¦¡£
ͬÑùµÀÀí£¬rownum Èç¹ûµ¥¶ÀÓÃ=,Ò²Ö»ÓÐÔÚrownum =1ʱ²ÅÓÐÓá£
¶ÔÓÚrownum À´ËµËüÊÇoracle ϵͳ˳Ðò·ÖÅäΪ´Ó²éѯ·µ»ØµÄÐеıàºÅ£¬·µ»ØµÄµÚÒ»ÐзÖÅäµÄÊÇ1£¬µÚ¶þÐÐÊÇ2£¬ÒÀ´ËÀàÍÆ£¬Õâ¸öα×ֶοÉÒÔÓÃÓÚÏÞÖÆ²éѯ·µ»ØµÄ×ÜÐÐÊý£¬¶øÇÒrownum ²»ÄÜÒ ......
µ¥±í²åÈëÒÔinsert into¿ªÍ·,²»ÄÜÓÐthen intoÓï¾ä.
¶à±í²åÈëÒÔinsert first/all ¿ªÍ·,¿ÉÒÔÓÐthen intoÓï¾ä
ÔÚOracle²Ù×÷¹ý³ÌÖо³£»áÓöµ½Í¬Ê±Ïò¶à¸ö²»Í¬µÄ±í²åÈëÊý¾Ý£¬´ËʱÓøÃÓï¾ä¾Í·Ç³£ºÏÊÊ¡£
All±íʾ·Ç¶Ì·ÔËË㣬¼´Âú×ãÁ˵ÚÒ»¸öÌõ¼þÒ²µÃÏòÏÂÖ´Ðв鿴ÊÇ·ñÂú×ãÆäËüÌõ¼þ£¬¶øFirstÊǶÌ·ÔËËãÕÒµ½ºÏÊÊÌõ¼þ¾Í²»ÏòϽøÐС£
INSERT ALL
WHEN prod_category=’B’ THEN
INTO book_sales(prod_id,cust_id,qty_sold,amt_sold)
VALUES(product_id,customer_id,sale_qty,sale_price)
WHEN prod_category=’V’ THEN
INTO video_sales(prod_id,cust_id,qty_sold,amt_sold)
VALUES(product_id,customer_id,sale_qty,sale_price)
WHEN prod_category=’A’ THEN
INTO audio_sales(prod_id,cust_id,qty_sold,amt_sold)
VALUES(product_id,customer_id,sale_qty,sale_price)
SELECT prod_category ,product_id ,customer_id ,sale_qty
,sale_price
from sales_detail;
Merging Rows into a Table
MERGE INTO oe.product_information pi
USING (SELECT product_id, list_price, min_price
from new_prices) NP
ON (pi.product_id = ......
Êý¾Ý¿â²éѯÖÐÄÑÃâ»áÓöµ½ÐÐÁÐת»»µÄÇé¿ö£¬ÕªÁÐһЩ½Ï¾«µäµÄ½â¾ö·½°¸
--####################################################################
Ò»¡¢²ÉÓÃSQL decodeºÍPL/SQLº¯ÊýʵÏÖ
--####################################################################
1¡¢¹Ì¶¨ÁÐÊýµÄÐÐÁÐת»»
Èç
student subject grade
---------------------------
student1 ÓïÎÄ 80
student1 Êýѧ 70
student1 Ó¢Óï 60
student2 ÓïÎÄ 90
student2 Êýѧ 80
student2 Ó¢Óï 100
……
ת»»Îª
ÓïÎÄ Êýѧ Ó¢Óï
student1 80 70 60
student2 90 80 100
……
Óï¾äÈçÏ£º
select student,sum(decode(subject,'ÓïÎÄ', grade,null)) "ÓïÎÄ",
sum(decode(subject,'Êýѧ', grade,null)) "Êýѧ",
sum(decode(subject,'Ó¢Óï', grade,null)) "Ó¢Óï"
from table
group by student
2¡¢²»¶¨ÁÐÐÐÁÐת»»
Èç
c1 c2
--------------
1 ÎÒ
1 ÊÇ
1 Ë
2 Öª
2 µÀ
3 ²»
……
ת»»Îª
1 ÎÒÊÇË
2 ÖªµÀ
3 ²»
ÕâÒ»ÀàÐ͵Äת»»±ØÐë½èÖúÓÚPL/SQLÀ´Íê³É£¬ÕâÀï¸øÒ»¸öÀý×Ó
CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)
RETURN VARCHAR2
IS
Col_ ......
<!--
/* Font Definitions */
@font-face
{font-family:ËÎÌå;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@ËÎÌå";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:ËÎÌå;
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72 ......
Á·Ï°:
drop table Employee;
create table Employee(
id number primary key,
uName varchar2(32) ,
dept_id int ,
salary number
);
insert into Employee (id ,uName, dept_id, salary ) values (1,'ZhangShan',null, 10000);
insert into Employee (id ,uName, dept_id, salary ) values (2,'LiShi',1, 8000);
insert into&nbs ......