注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 飞天兔 于 2021-8-22 01:31 编辑 .1point3acres
LZ平时不用SQL,单纯为了面试把所有LC刷了两编,有挑战题的其实就两种: 窗口函数和自定义函数。自定义函数面试中实用性不高,用的时候容易出错。. ----
但是如果窗口函数不熟练,遇到就会跪,而且是立刻马上,拉都拉不起来。.--
LZ从LC挑出八道代表题,附上我的答案和考点,值得反复练习。结尾处附上窗口函数的cheetsheet。. 1point3acres.com
据说给大米的最后都拿到大包
1. Median (LC 571)
# Median选判条件- select avg(a.number) as median
- from(. 1point 3 acres
- select number, . 1point 3acres
- sum(Frequency) over(order by number) as pc,
- sum(Frequency) over(order by number desc) as nc,
- sum(Frequency) over() as tt
- rom numbers
- as a
- where a.pc>=tt/2 and a.nc>=tt/2
复制代码 2. TOP N (LC 185)
# Row_number (). rank (), 和dense_rank () 区别- with tp as
- Select Name, Salary, DepartmentId,
- dense_rank() over(partition by DepartmentId order by Salary desc) as top .--
- from Employee)
- select d.Name as Department,tp.Name as Employee,tp.Salary
- from tp
- join Department as d
- on tp.DepartmentId = d.Id
- where tp.top<4
复制代码 3. Consecutive Numbers (LC 180)
#主键ID不连续
# DF对于连续数字相等 - SELECT DISTINCT Num AS CN
- FROM (
- SELECT *, ROW_NUMBER () OVER (ORDER BY Id) –
- ROW_NUMBER () OVER (PARTITION BY Num ORDER BY Id) AS DF .
- FROM Logs
- ) as TP
- GROUP BY Num, DF . Waral dи,
- HAVING COUNT (1) >=3
复制代码 4. Continuous Ranges (LC 1285) .--
# 连续问题选用lead/lag 函数 或者 id-row_number- SELECT
- MIN(log_id) START_ID,
- MAX(log_id) END_ID
- FROM (
- SELECT
- log_id,
- log_id-ROW_NUMBER () OVER (ORDER BY log_id) DF
- FROM Logs. 1point3acres
- ) AS TP
- GROUP BY DF
- ORDER BY 1
复制代码 5. Consecutive Available Seats (LC 603)
# 连续问题选用lead/lag 函数 或者 id-row_number- select distinct seat_id
- from (
- select *,
- lag(free,1,0) over(order by seat_id) as lg,. 1point3acres.com
- lead(free,1,0) over(order by seat_id) as ld
- from cinema
- ) as TP
- where free=1 and (lg=1 or ld=1)
- order by 1
复制代码 6. Students Report by Geography (LC 618)
# Row_number () 给列中元素编号
# Group by + 聚合函数 行转列- select
- MAX (if (continent="America", name, NULL)) as America,.1point3acres
- MAX (if (continent="Asia", name, NULL)) as Asia,
- MAX (if (continent="Europe", name, NULL)) as Europe
- from (
- select *,
- row_number () over (partition by continent order by name) as rk. check 1point3acres for more.
- from student
- ) as TP. 1point3acres.com
- group by rk
- order by rk
复制代码 7. Students Report by Geography (LC 618)
# Default window: range between unbounded preceding AND current row- select person_name
- from (
- select person_name,
- sum(weight) over(order by turn) as tt. From 1point 3acres bbs
- from queue
- order by turn
- ) as tp
- where tt<=1000
- order by tt desc
- limit 1
复制代码 8. Restaurant Growth (LC 1321)
# Window w 的写法
# Range是逻辑窗口,row是物理窗口,这里要用物理窗口. 1point 3acres
# limit 6, 100000 # Top 6+ 前六天不算# 窗口函数因为计算问题是不能在where,join中引用的,但是可以在外面套别的函数,比如这题avg外面用了round函数,if函数也是可以的
- select visited_on,
- sum(tt) OVER w as amount,-baidu 1point3acres
- round(avg(tt) OVER w,2) as average_amount
- from (
- select visited_on, sum(amount) as tt
- from customer
- group by 1
- ) as tp
- window w as (ORDER BY VISITED_ON rows between 6 PRECEDING)
- limit 6,100000
复制代码 收藏的窗口函数cheetsheet,直接贴链接,不消耗大米
https://learnsql.com/blog/sql-window-functions-cheat-sheet/
. 1point 3 acres
-baidu 1point3acres
.google и
补充内容 (2021-08-30 05:40 +8:00):
第七题 标题写错了
Last Person to Fit in the Bus (LC 1204) |