测试脚本
create table movies
( year_of_release number(4),
name varchar2(60),
gross_sales number(12)
);
insert into movies (year_of_release,name,gross_sales) values (1977,'star wars ep. iv: a new hope',786535665);
insert into movies (year_of_release,name,gross_sales) values (1982,'et: the extra-terrestrial',792804231);
insert into movies (year_of_release,name,gross_sales) values (1993,'jurassic park',1038812584);
insert into movies (year_of_release,name,gross_sales) values (1994,'forrest gump',679857164);
insert into movies (year_of_release,name,gross_sales) values (1994,'the lion king',987480140);
insert into movies (year_of_release,name,gross_sales) values (1996,'independence day',817355682);
insert into movies (year_of_release,name,gross_sales) values (1997,'the lost world: jurassic park',618626844);
insert into movies (year_of_release,name,gross_sales) values (1997,'titanic',2207615668);
insert into movies (year_of_release,name,gross_sales) values (1999,'star wars ep. i: the phantom menace',1027044677);
insert into movies (year_of_release,name,gross_sales) values (1999,'the sixth sense',672806292);
insert into movies (year_of_release,name,gross_sales) values (2001,'harry potter and the sorcerer''s stone',974755371);
insert into movies (year_of_release,name,gross_sales) values (2001,'the lord of the rings: the fellowship of the ring',887217688);
insert into movies (year_of_release,name,gross_sales) values (2002,'harry potter and the chamber of secrets',878979634);
insert into movies (year_of_release,name,gross_sales) values (2002,'spider-man',821706375);
insert into movies (year_of_release,name,gross_sales) values (2002,'star wars ep. ii: attack of the clones',648200000);
insert into movies (year_of_release,name,gross_sales) values (2002,'the lord of the rings: the two towers',934703179);
insert into movies (year_of_release,name,gross_sales) values (2003,'finding nemo',936429370);
insert into movies (year_of_release,name,gross_sales) values (2003,'pirates of the caribbean: the curse of the black pearl',634954103);
insert into movies (year_of_release,name,gross_sales) values (2003,'the lord of the rings: the return of the king',1141408667);
insert into movies (year_of_release,name,gross_sales) values (2003,'the matrix reloaded',738576929);
insert into movies (year_of_release,name,gross_sales) values (2004,'harry potter and the prisoner of azkaban',796688549);
insert into movies (year_of_release,name,gross_sales) values (2004,'shrek 2',932252921);
insert into movies (year_of_release,name,gross_sales) values (2004,'spider-man 2',783705001);
insert into movies (year_of_release,name,gross_sales) values (2004,'the passion of the christ',622420667);
insert into movies (year_of_release,name,gross_sales) values (2005,'harry potter and the goblet of fire',896911078);
insert into movies (year_of_release,name,gross_sales) values (2005,'star wars ep. iii: revenge of the sith',848998877);
insert into movies (year_of_release,name,gross_sales) values (2005,'the chronicles of narnia',720539572);
insert into movies (year_of_release,name,gross_sales) values (2006,'ice age: the meltdown',651899282);
insert into movies (year_of_release,name,gross_sales) values (2006,'pirates of the caribbean: dead man''s chest',1066215812);
insert into movies (year_of_release,name,gross_sales) values (2006,'the da vinci code',767820459);
insert into movies (year_of_release,name,gross_sales) values (2007,'harry potter and the order of the phoenix',942943935);
insert into movies (year_of_release,name,gross_sales) values (2007,'pirates of the caribbean: at world''s end',963420425);
insert into movies (year_of_release,name,gross_sales) values (2007,'ratatouille',626549695);
insert into movies (year_of_release,name,gross_sales) values (2007,'shrek the third',805623351);
insert into movies (year_of_release,name,gross_sales) values (2007,'spider-man 3',890875303);
insert into movies (year_of_release,name,gross_sales) values (2007,'transformers',708098205);
insert into movies (year_of_release,name,gross_sales) values (2008,'hancock',624234272);
insert into movies (year_of_release,name,gross_sales) values (2008,'indiana jones and the kingdom of the crystal skull',786558145);
insert into movies (year_of_release,name,gross_sales) values (2008,'kung fu panda',631910531);
insert into movies (year_of_release,name,gross_sales) values (2008,'the dark knight',1002891358);
insert into movies (year_of_release,name,gross_sales) values (2009,'2012',788408539);
insert into movies (year_of_release,name,gross_sales) values (2009,'avatar',2783918982);
insert into movies (year_of_release,name,gross_sales) values (2009,'harry potter and the half-blood prince',935083686);
insert into movies (year_of_release,name,gross_sales) values (2009,'ice age: dawn of the dinosaurs',859701857);
insert into movies (year_of_release,name,gross_sales) values (2009,'the twilight saga: new moon',687557727);
insert into movies (year_of_release,name,gross_sales) values (2009,'transformers: revenge of the fallen',836519699);
insert into movies (year_of_release,name,gross_sales) values (2009,'up',731542621);
insert into movies (year_of_release,name,gross_sales) values (2010,'alice in wonderland',1025491110);
insert into movies (year_of_release,name,gross_sales) values (2010,'harry potter and the deathly hallows: part i',959301070);
insert into movies (year_of_release,name,gross_sales) values (2010,'inception',832584416);
insert into movies (year_of_release,name,gross_sales) values (2010,'iron man 2',623256345);
insert into movies (year_of_release,name,gross_sales) values (2010,'shrek forever after',755903876);
insert into movies (year_of_release,name,gross_sales) values (2010,'the twilight saga: eclipse',706102828);
insert into movies (year_of_release,name,gross_sales) values (2010,'toy story 3',1069818229);
insert into movies (year_of_release,name,gross_sales) values (2011,'fast five',629969804);
insert into movies (year_of_release,name,gross_sales) values (2011,'harry potter and the deathly hallows: part ii',1341511219);
insert into movies (year_of_release,name,gross_sales) values (2011,'kung fu panda 2',664837547);
insert into movies (year_of_release,name,gross_sales) values (2011,'mission: impossible - ghost protocol',694713230);
insert into movies (year_of_release,name,gross_sales) values (2011,'pirates of the caribbean: on stranger tides',1045663875);
insert into movies (year_of_release,name,gross_sales) values (2011,'the twilight saga: breaking dawn, part 1',689420051);
insert into movies (year_of_release,name,gross_sales) values (2011,'transformers: dark of the moon',1123790543);
insert into movies (year_of_release,name,gross_sales) values (2012,'ice age: continental drift',879765137);
insert into movies (year_of_release,name,gross_sales) values (2012,'madagascar 3: europe''s most wanted',746921271);
insert into movies (year_of_release,name,gross_sales) values (2012,'men in black 3',654213485);
insert into movies (year_of_release,name,gross_sales) values (2012,'skyfall',1110526981);
insert into movies (year_of_release,name,gross_sales) values (2012,'the amazing spider-man',757890267);
insert into movies (year_of_release,name,gross_sales) values (2012,'the avengers',1519479547);
insert into movies (year_of_release,name,gross_sales) values (2012,'the dark knight rises',1084439099);
insert into movies (year_of_release,name,gross_sales) values (2012,'the hobbit: an unexpected journey',1017003568);
insert into movies (year_of_release,name,gross_sales) values (2012,'the hunger games',677923379);
insert into movies (year_of_release,name,gross_sales) values (2012,'the twilight saga: breaking dawn, part 2',829724737);
insert into movies (year_of_release,name,gross_sales) values (2013,'despicable me 2',974873764);
insert into movies (year_of_release,name,gross_sales) values (2013,'fast and furious 6',789952811);
insert into movies (year_of_release,name,gross_sales) values (2013,'frozen',1274234980);
insert into movies (year_of_release,name,gross_sales) values (2013,'gravity',716392705);
insert into movies (year_of_release,name,gross_sales) values (2013,'iron man 3',1215392272);
insert into movies (year_of_release,name,gross_sales) values (2013,'man of steel',667999518);
insert into movies (year_of_release,name,gross_sales) values (2013,'monsters university',743588329);
insert into movies (year_of_release,name,gross_sales) values (2013,'the hobbit: the desolation of smaug',960366855);
insert into movies (year_of_release,name,gross_sales) values (2013,'the hunger games: catching fire',864868047);
insert into movies (year_of_release,name,gross_sales) values (2013,'thor: the dark world',633360018);
insert into movies (year_of_release,name,gross_sales) values (2014,'big hero 6',652127828);
insert into movies (year_of_release,name,gross_sales) values (2014,'captain america: the winter soldier',713846958);
insert into movies (year_of_release,name,gross_sales) values (2014,'dawn of the planet of the apes',703545589);
insert into movies (year_of_release,name,gross_sales) values (2014,'guardians of the galaxy',771172112);
insert into movies (year_of_release,name,gross_sales) values (2014,'how to train your dragon 2',616102924);
insert into movies (year_of_release,name,gross_sales) values (2014,'interstellar',665417894);
insert into movies (year_of_release,name,gross_sales) values (2014,'maleficent',758536735);
insert into movies (year_of_release,name,gross_sales) values (2014,'the amazing spider-man 2',708996336);
insert into movies (year_of_release,name,gross_sales) values (2014,'the hobbit: the battle of the five armies',955119788);
insert into movies (year_of_release,name,gross_sales) values (2014,'the hunger games: mockingjay - part 1',709635885);
insert into movies (year_of_release,name,gross_sales) values (2014,'transformers: age of extinction',1104039076);
insert into movies (year_of_release,name,gross_sales) values (2014,'x-men: days of future past',748121534);
insert into movies (year_of_release,name,gross_sales) values (2015,'furious 7',1515993181);
insert into movies (year_of_release,name,gross_sales) values (2015,'inside out',853031215);
insert into movies (year_of_release,name,gross_sales) values (2015,'jurassic world',1666248032);
insert into movies (year_of_release,name,gross_sales) values (2015,'minions',1163530631);
insert into movies (year_of_release,name,gross_sales) values (2015,'mission: impossible - rogue nation',700868363);
insert into movies (year_of_release,name,gross_sales) values (2015,'spectre',821980199);
insert into movies (year_of_release,name,gross_sales) values (2015,'the avengers: age of ultron',1404705868);
alter table movies add primary key ( name );
select count(*) from movies
count(*)
100
CUME_DIST
计算一组值中某个值的累积分布。返回值的范围(0,1],计算方法:RANK/N
">
">">https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/CUME_DIST.html#GUID-B12C577C-A63C-4D19-8E18-FCCBBFBF8278
SQL> select name, gross_sales,100*cume_dist() over ( order by gross_sales ) as cumedist from movies order by 2,name;
NAME GROSS_SALES CUMEDIST
------------------------------------------------------------ ----------- ----------
How to Train Your Dragon 2 616102924 1
The Lost World: Jurassic Park 618626844 2
The Passion of the Christ 622420667 3
Iron Man 2 623256345 4
Hancock 624234272 5
Ratatouille 626549695 6
Fast Five 629969804 7
Kung Fu Panda 631910531 8
Thor: The Dark World 633360018 9
Pirates of the Caribbean: The Curse of the Black Pearl 634954103 10
Star Wars Ep. II: Attack of the Clones 648200000 11
Ice Age: The Meltdown 651899282 12
Big Hero 6 652127828 13
Men in Black 3 654213485 14
Kung Fu Panda 2 664837547 15
Interstellar 665417894 16
Man of Steel 667999518 17
The Sixth Sense 672806292 18
The Hunger Games 677923379 19
Forrest Gump 679857164 20
The Twilight Saga: New Moon 687557727 21
The Twilight Saga: Breaking Dawn, Part 1 689420051 22
Mission: Impossible - Ghost Protocol 694713230 23
Mission: Impossible - Rogue Nation 700868363 24
Dawn of the Planet of the Apes 703545589 25
The Twilight Saga: Eclipse 706102828 26
Transformers 708098205 27
The Amazing Spider-Man 2 708996336 28
The Hunger Games: Mockingjay - Part 1 709635885 29
Captain America: The Winter Soldier 713846958 30
Gravity 716392705 31
The Chronicles of Narnia 720539572 32
Up 731542621 33
The Matrix Reloaded 738576929 34
Monsters University 743588329 35
Madagascar 3: Europe's Most Wanted 746921271 36
X-Men: Days of Future Past 748121534 37
Shrek Forever After 755903876 38
The Amazing Spider-Man 757890267 39
Maleficent 758536735 40
The Da Vinci Code 767820459 41
Guardians of the Galaxy 771172112 42
Spider-Man 2 783705001 43
Star Wars Ep. IV: A New Hope 786535665 44
Indiana Jones and the Kingdom of the Crystal Skull 786558145 45
2012 788408539 46
Fast and Furious 6 789952811 47
ET: The Extra-Terrestrial 792804231 48
Harry Potter and the Prisoner of Azkaban 796688549 49
Shrek the Third 805623351 50
Independence Day 817355682 51
Spider-Man 821706375 52
Spectre 821980199 53
The Twilight Saga: Breaking Dawn, Part 2 829724737 54
Inception 832584416 55
Transformers: Revenge of the Fallen 836519699 56
Star Wars Ep. III: Revenge of the Sith 848998877 57
Inside Out 853031215 58
Ice Age: Dawn of the Dinosaurs 859701857 59
The Hunger Games: Catching Fire 864868047 60
Harry Potter and the Chamber of Secrets 878979634 61
Ice Age: Continental Drift 879765137 62
The Lord of the Rings: The Fellowship of the Ring 887217688 63
Spider-Man 3 890875303 64
Harry Potter and the Goblet of Fire 896911078 65
Shrek 2 932252921 66
The Lord of the Rings: The Two Towers 934703179 67
Harry Potter and the Half-Blood Prince 935083686 68
Finding Nemo 936429370 69
Harry Potter and the Order of the Phoenix 942943935 70
The Hobbit: The Battle of the Five Armies 955119788 71
Harry Potter and the Deathly Hallows: Part I 959301070 72
The Hobbit: The Desolation of Smaug 960366855 73
Pirates of the Caribbean: At World's End 963420425 74
Harry Potter and the Sorcerer's Stone 974755371 75
Despicable Me 2 974873764 76
The Lion King 987480140 77
The Dark Knight 1002891358 78
The Hobbit: An Unexpected Journey 1017003568 79
Alice in Wonderland 1025491110 80
Star Wars Ep. I: The Phantom Menace 1027044677 81
Jurassic Park 1038812584 82
Pirates of the Caribbean: On Stranger Tides 1045663875 83
Pirates of the Caribbean: Dead Man's Chest 1066215812 84
Toy Story 3 1069818229 85
The Dark Knight Rises 1084439099 86
Transformers: Age of Extinction 1104039076 87
Skyfall 1110526981 88
Transformers: Dark of the Moon 1123790543 89
The Lord of the Rings: The Return of the King 1141408667 90
Minions 1163530631 91
Iron Man 3 1215392272 92
Frozen 1274234980 93
Harry Potter and the Deathly Hallows: Part II 1341511219 94
The Avengers: Age of Ultron 1404705868 95
Furious 7 1515993181 96
The Avengers 1519479547 97
Jurassic World 1666248032 98
Titanic 2207615668 99
Avatar 2783918982 100
PERCENT_RANK
返回ORDER BY 子句所指定列中的分布值,取值范围:[0,1],计算方法为 (RANK - 1)/(N- 1)
">
">">https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/PERCENT_RANK.html#GUID-66A868F5-9EBA-482A-BF8C-09300B9EE165
SQL> select name, gross_sales,100*percent_rank() over ( order by gross_sales ) as pctrank from movies order by 2,name;
NAME GROSS_SALES PCTRANK
------------------------------------------------------------ ----------- ----------
How to Train Your Dragon 2 616102924 0
The Lost World: Jurassic Park 618626844 1.01010101
The Passion of the Christ 622420667 2.02020202
Iron Man 2 623256345 3.03030303
Hancock 624234272 4.04040404
Ratatouille 626549695 5.05050505
Fast Five 629969804 6.06060606
Kung Fu Panda 631910531 7.07070707
Thor: The Dark World 633360018 8.08080808
Pirates of the Caribbean: The Curse of the Black Pearl 634954103 9.09090909
Star Wars Ep. II: Attack of the Clones 648200000 10.1010101
Ice Age: The Meltdown 651899282 11.1111111
Big Hero 6 652127828 12.1212121
Men in Black 3 654213485 13.1313131
Kung Fu Panda 2 664837547 14.1414141
Interstellar 665417894 15.1515152
Man of Steel 667999518 16.1616162
The Sixth Sense 672806292 17.1717172
The Hunger Games 677923379 18.1818182
Forrest Gump 679857164 19.1919192
The Twilight Saga: New Moon 687557727 20.2020202
The Twilight Saga: Breaking Dawn, Part 1 689420051 21.2121212
Mission: Impossible - Ghost Protocol 694713230 22.2222222
Mission: Impossible - Rogue Nation 700868363 23.2323232
Dawn of the Planet of the Apes 703545589 24.2424242
The Twilight Saga: Eclipse 706102828 25.2525253
Transformers 708098205 26.2626263
The Amazing Spider-Man 2 708996336 27.2727273
The Hunger Games: Mockingjay - Part 1 709635885 28.2828283
Captain America: The Winter Soldier 713846958 29.2929293
Gravity 716392705 30.3030303
The Chronicles of Narnia 720539572 31.3131313
Up 731542621 32.3232323
The Matrix Reloaded 738576929 33.3333333
Monsters University 743588329 34.3434343
Madagascar 3: Europe's Most Wanted 746921271 35.3535354
X-Men: Days of Future Past 748121534 36.3636364
Shrek Forever After 755903876 37.3737374
The Amazing Spider-Man 757890267 38.3838384
Maleficent 758536735 39.3939394
The Da Vinci Code 767820459 40.4040404
Guardians of the Galaxy 771172112 41.4141414
Spider-Man 2 783705001 42.4242424
Star Wars Ep. IV: A New Hope 786535665 43.4343434
Indiana Jones and the Kingdom of the Crystal Skull 786558145 44.4444444
2012 788408539 45.4545455
Fast and Furious 6 789952811 46.4646465
ET: The Extra-Terrestrial 792804231 47.4747475
Harry Potter and the Prisoner of Azkaban 796688549 48.4848485
Shrek the Third 805623351 49.4949495
Independence Day 817355682 50.5050505
Spider-Man 821706375 51.5151515
Spectre 821980199 52.5252525
The Twilight Saga: Breaking Dawn, Part 2 829724737 53.5353535
Inception 832584416 54.5454545
Transformers: Revenge of the Fallen 836519699 55.5555556
Star Wars Ep. III: Revenge of the Sith 848998877 56.5656566
Inside Out 853031215 57.5757576
Ice Age: Dawn of the Dinosaurs 859701857 58.5858586
The Hunger Games: Catching Fire 864868047 59.5959596
Harry Potter and the Chamber of Secrets 878979634 60.6060606
Ice Age: Continental Drift 879765137 61.6161616
The Lord of the Rings: The Fellowship of the Ring 887217688 62.6262626
Spider-Man 3 890875303 63.6363636
Harry Potter and the Goblet of Fire 896911078 64.6464646
Shrek 2 932252921 65.6565657
The Lord of the Rings: The Two Towers 934703179 66.6666667
Harry Potter and the Half-Blood Prince 935083686 67.6767677
Finding Nemo 936429370 68.6868687
Harry Potter and the Order of the Phoenix 942943935 69.6969697
The Hobbit: The Battle of the Five Armies 955119788 70.7070707
Harry Potter and the Deathly Hallows: Part I 959301070 71.7171717
The Hobbit: The Desolation of Smaug 960366855 72.7272727
Pirates of the Caribbean: At World's End 963420425 73.7373737
Harry Potter and the Sorcerer's Stone 974755371 74.7474747
Despicable Me 2 974873764 75.7575758
The Lion King 987480140 76.7676768
The Dark Knight 1002891358 77.7777778
The Hobbit: An Unexpected Journey 1017003568 78.7878788
Alice in Wonderland 1025491110 79.7979798
Star Wars Ep. I: The Phantom Menace 1027044677 80.8080808
Jurassic Park 1038812584 81.8181818
Pirates of the Caribbean: On Stranger Tides 1045663875 82.8282828
Pirates of the Caribbean: Dead Man's Chest 1066215812 83.8383838
Toy Story 3 1069818229 84.8484848
The Dark Knight Rises 1084439099 85.8585859
Transformers: Age of Extinction 1104039076 86.8686869
Skyfall 1110526981 87.8787879
Transformers: Dark of the Moon 1123790543 88.8888889
The Lord of the Rings: The Return of the King 1141408667 89.8989899
Minions 1163530631 90.9090909
Iron Man 3 1215392272 91.9191919
Frozen 1274234980 92.9292929
Harry Potter and the Deathly Hallows: Part II 1341511219 93.9393939
The Avengers: Age of Ultron 1404705868 94.9494949
Furious 7 1515993181 95.959596
The Avengers 1519479547 96.969697
Jurassic World 1666248032 97.979798
Titanic 2207615668 98.989899
Avatar 2783918982 100
ntile
函数把记录结果集分成N部分
https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/NTILE.html
SQL> select name, gross_sales,ntile(4) over ( order by gross_sales ) as quartile from movies where rownum<8 order by 2,name;
NAME GROSS_SALES QUARTILE
------------------------------------------------------------ ----------- ----------
The Lost World: Jurassic Park 618626844 1
Forrest Gump 679857164 1
Star Wars Ep. IV: A New Hope 786535665 2
ET: The Extra-Terrestrial 792804231 2
Independence Day 817355682 3
The Lion King 987480140 3
Jurassic Park 1038812584 4