sql(oracle)數(shù)據(jù)處理實用總結(jié)開窗函數(shù)(over partition)使用

開窗函數(shù)over partition

select test_name, test_id, n_rank, test_salary_sum
  from (select t_alias.test_name,
               t_alias.test_id,
               sum(t_alias.test_salary) test_salary_sum,
               row_number() over(partition by t_alias.test_name order by sum(t_alias.test_salary) desc) n_rank
          from (select 1 test_id, 'name_1' test_name, 100 test_salary
                  from dual
                union all
                select 2 test_id, 'name_1' test_name, 200 test_salary
                  from dual
                union all
                select 2 test_id, 'name_1' test_name, 300 test_salary
                  from dual
                union all
                select 3 test_id, 'name_1' test_name, 300 test_salary
                  from dual
                union all
                select 4 test_id, 'name_1' test_name, 400 test_salary
                  from dual
                union all
                select 1 test_id, 'name_2' test_name, 200 test_salary
                  from dual
                union all
                select 2 test_id, 'name_2' test_name, 200 test_salary
                  from dual
                union all
                select 2 test_id, 'name_2' test_name, 200 test_salary
                  from dual
                union all
                select 3 test_id, 'name_2' test_name, 400 test_salary
                  from dual
                union all
                select 4 test_id, 'name_2' test_name, 400 test_salary
                  from dual) t_alias
         group by t_alias.test_name, t_alias.test_id)
 where n_rank <= 2
 order by test_name, n_rank;

開窗函數(shù)使用于取出多列分組察迟,取一列分組下另一組前幾名隐绵,先利用開窗函數(shù)對其分組排名愧怜,開窗函數(shù)排名函數(shù)較多使用row_number(),還有rank()等则奥,生成排名列之后將結(jié)果集篩選其排名前幾<=2或者你想要的前幾名考润。
上述執(zhí)行結(jié)果:


image-the result of above example

獲得每個test_name字段下test_id的test_salary的前2名。
開窗函數(shù)實例:


image-實例

另有開窗函數(shù)替代寫法,可以了解:
select * from adminus;
create table adminus_test(num number(5,2),count number(5,2));
insert into adminus_test(num,count) values(1,5);
insert into adminus_test(num,count) values(1,7);
insert into adminus_test(num,count) values(1,8);
insert into adminus_test(num,count) values(2,8);
insert into adminus_test(num,count) values(2,9);
insert into adminus_test(num,count) values(2,7);
select * from adminus_test;
--開窗函數(shù)替代寫法
select d.admin_id, d.num, c.count
  from (select num,
               count,
               (select count(1)
                  from adminus_test b
                 where b.num = a.num
                   and a.count <= b.count) as cnt --等級的變相的寫法 --重要寫法
          from adminus_test a
         group by num, count) c
  join adminus d
    on c.num = d.num
 where c.cnt <= 2;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末读处,一起剝皮案震驚了整個濱河市糊治,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌罚舱,老刑警劉巖井辜,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異馆匿,居然都是意外死亡,警方通過查閱死者的電腦和手機燥滑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門渐北,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人铭拧,你說我怎么就攤上這事赃蛛。” “怎么了搀菩?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵呕臂,是天一觀的道長。 經(jīng)常有香客問我肪跋,道長歧蒋,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任州既,我火速辦了婚禮谜洽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘吴叶。我一直安慰自己阐虚,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布蚌卤。 她就那樣靜靜地躺著实束,像睡著了一般。 火紅的嫁衣襯著肌膚如雪逊彭。 梳的紋絲不亂的頭發(fā)上咸灿,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天,我揣著相機與錄音侮叮,去河邊找鬼析显。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的谷异。 我是一名探鬼主播分尸,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼歹嘹!你這毒婦竟也來了箩绍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤尺上,失蹤者是張志新(化名)和其女友劉穎材蛛,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體怎抛,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡卑吭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了马绝。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豆赏。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖富稻,靈堂內(nèi)的尸體忽然破棺而出掷邦,到底是詐尸還是另有隱情,我是刑警寧澤椭赋,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布抚岗,位于F島的核電站,受9級特大地震影響哪怔,放射性物質(zhì)發(fā)生泄漏宣蔚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一认境、第九天 我趴在偏房一處隱蔽的房頂上張望件已。 院中可真熱鬧,春花似錦元暴、人聲如沸篷扩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鉴未。三九已至,卻和暖如春鸠姨,著一層夾襖步出監(jiān)牢的瞬間铜秆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工讶迁, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留连茧,地道東北人。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像啸驯,于是被迫代替她去往敵國和親客扎。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容