02. SQLboy自連接實現(xiàn)窗口用法

視頻篇文檔

自連接實現(xiàn)窗口用法

sum() avg() count() over(PARTITION BY)
lag lead 
row_number dense_rank rank
....

sum() over()

案例一 :累積消費

create table mytable_0719(
uid int ,
event_date date,
consumption  double
);

INSERT INTO mytable_0719 (uid,event_date,consumption) VALUES
     (1,'2023-07-01',10.0),
     (1,'2023-07-02',10.0),
     (1,'2023-07-03',20.0),
     (1,'2023-07-04',10.0),
     (1,'2023-07-05',10.0),
     (2,'2023-07-04',10.0),
     (2,'2023-07-05',10.0),
     (3,'2023-07-05',10.0);
select uid,event_date,consumption, sum(consumption) over(PARTITION BY uid ORDER BY  event_date asc) as csm  from mytable_0719
自連接用法
select
t1.uid , t1.event_date,sum(t2.consumption) csm  
from 
mytable_0719 t1 join mytable_0719 t2
on t1.uid = t2.uid and t1.event_date >= t2.event_date
group by t1.uid , t1.event_date

案例二 : 查詢最近一個月(即最大月)之外陈莽,剩下每個月的近三個月的累計薪水(不足三個月也要計算)

create table mytable_0720(
uid int ,
month  int,
salary  double
);

INSERT INTO mytable_0720 (uid,month,salary) VALUES
 (1,1,20),
 (2,1,20),
 (1,2,30),
 (2,2,30),
 (3,2,40),
 (1,3,40),
 (3,3,60),
 (1,4,60),
 (3,4,70);
 
 
 結(jié)果
1   3   90.0
1   2   50.0
1   1   20.0
2   1   20.0
3   3   100.0
3   2   40.0
select
    uid,
    month,
    sum(salary) over(partition by uid
order by
    month rows between 2 preceding and current row ) Salary
from
    (
    select
        uid,
        month,
        salary,
        lag(salary, 1, 0) over(partition by uid
    order by
        month desc) rn
    from
        mytable_0720
) t1
where
    rn != 0
order by
    uid,
    month desc
select
    a.uid as uid,
    a.month as month,
    SUM(b.salary) as salary
from
    mytable_0720 a,
    mytable_0720 b
where
    a.uid = b.uid
    and a.month >= b.month
    and a.month < b.month + 3
    and (a.uid,
    a.month) not in (
    select
        uid,
        MAX(month)
    from
        mytable_0720
    group by
        uid)
group by
    a.uid,
    a.month
order by
    a.uid,
    a.month desc
TOP_N問題

沿用mytable_0719

create table mytable_0719(
uid int ,
event_date date,
consumption  double
);

案例一: 計算每個uid,每天消費金額的消費排名,相同消費金額的并列

select
t1.uid ,t1.event_date,t1.consumption,count(distinct t2.consumption) as t_rank 
from
mytable_0719 t1  join mytable_0719 t2 
on t1.consumption <= t2.consumption and  t1.uid =  t2.uid
group by t1.uid ,t1.event_date,t1.consumption
order by t_rank

案例二:取前二

select
t1.uid ,t1.event_date,t1.consumption,count(distinct t2.consumption) as t_rank 
from
mytable_0719 t1  join mytable_0719 t2 
on t1.consumption <= t2.consumption and  t1.uid =  t2.uid
group by t1.uid ,t1.event_date,t1.consumption
order by t_rank
havingcount(distinct t2.consumption) <= 2

案例三:取第二

select
t1.uid ,t1.event_date,t1.consumption,count(distinct t2.consumption) as t_rank ,sum(t1.consumption <= t2.consumption)
from
mytable_0719 t1  join mytable_0719 t2 
on t1.consumption <= t2.consumption and  t1.uid =  t2.uid
group by t1.uid ,t1.event_date,t1.consumption
having sum(t1.consumption <= t2.consumption)  = 2
order by t_rank
leedcode579
leedcode1159  
藕辜瘢客網(wǎng)23
牛客網(wǎng)74
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末脸侥,一起剝皮案震驚了整個濱河市建邓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌睁枕,老刑警劉巖官边,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異外遇,居然都是意外死亡注簿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門跳仿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诡渴,“玉大人,你說我怎么就攤上這事塔嬉⊥婊玻” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵谨究,是天一觀的道長恩袱。 經(jīng)常有香客問我,道長胶哲,這世上最難降的妖魔是什么畔塔? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮鸯屿,結(jié)果婚禮上澈吨,老公的妹妹穿的比我還像新娘。我一直安慰自己寄摆,他們只是感情好谅辣,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著婶恼,像睡著了一般桑阶。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上勾邦,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天蚣录,我揣著相機與錄音,去河邊找鬼眷篇。 笑死萎河,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播虐杯,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼玛歌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了擎椰?” 一聲冷哼從身側(cè)響起沾鳄,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎确憨,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瓤的,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡休弃,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了圈膏。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片塔猾。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖稽坤,靈堂內(nèi)的尸體忽然破棺而出丈甸,到底是詐尸還是另有隱情,我是刑警寧澤尿褪,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布睦擂,位于F島的核電站,受9級特大地震影響杖玲,放射性物質(zhì)發(fā)生泄漏顿仇。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一摆马、第九天 我趴在偏房一處隱蔽的房頂上張望臼闻。 院中可真熱鬧,春花似錦囤采、人聲如沸述呐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽乓搬。三九已至,卻和暖如春恕刘,著一層夾襖步出監(jiān)牢的瞬間缤谎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工褐着, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留坷澡,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓含蓉,卻偏偏與公主長得像频敛,于是被迫代替她去往敵國和親项郊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

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