0
点赞
收藏
分享

微信扫一扫

leetcode-sql-刷题汇总(175-180, 补充ing)

像小强一样活着 2022-04-27 阅读 78
leetcodesql

文章目录

leetcode-sql-刷题汇总

180. 连续出现的数字

思路1: 分组,把连续出现的相同数组分成一组,然后统计这组的个数,大于3则符合要求筛选出来

# 思路1: 分组,把连续出现的相同数组分成一组,然后统计这组的个数,大于3则符合要求筛选出来
select distinct num as ConsecutiveNums
from
(select num, group_id, 
count(1) as show_times
from
(select id, num, lag_num,
sum(if(lag_num = null or lag_num = num, 0, 1)) over (order by id) as group_id
from (
select id, 
num,
lag(num, 1) over (order by id) as lag_num
from `Logs`
) as t1 ) as t2
group by num, group_id) as t3
where show_times >= 3

思路2: 如果要求是3次,那么其实不用分组:只需要知道:某一行,改行前的num和当前相同,该行后num和当前相同,该num则符合要求

select distinct num as ConsecutiveNums 
from(
select id, num,
  lead(Num) over (order by Id) as nxt_num,
    lag(Num) over(order by Id) as lst_num
FROM Logs)t
where nxt_num=num and lst_num=num

178. 分数排名

思路1: dense_rank

# 思路1:
# 难道是没有这个函数吗
# 艹,rank是关键字。。。
select score, 
dense_rank() over (order by score desc) as `rank`
from Scores;

# select
#     score,
#     (dense_rank() over (order by Score desc)) AS "rank" 
# from
#     Scores

177. 第N高的薪水

思路1: row_number

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
        # 思路1
        select (select salary 
        from 
        (select salary, row_number() over (order by salary desc) as rn
        from (
        select DISTINCT salary from Employee) as r
        ) as t 
        where rn = N ) as SecondHighestSalary
  );
END

思路2: limit offset

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    SET N := N-1;
  RETURN (
        # 思路2
        # 注意:SET N := N-1;
        select (select DISTINCT salary
        from Employee
        order by salary DESC
        limit 1 offset N) as SecondHighestSalary
  );
END

思路3: 严谨点IFNULL

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    SET N := N-1;
  RETURN (
        # 思路3
        # 注意:SET N := N-1;
        select IFNULL((select DISTINCT salary
        from Employee
        order by salary DESC
        limit 1 offset N),NULL) as SecondHighestSalary
  );
END

思路4: group去重

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    SET N := N-1;
  RETURN (
        # 思路4:
        # 注意:SET N := N-1;
        SELECT 
                salary
        FROM 
                employee
        GROUP BY 
                salary
        ORDER BY 
                salary DESC
        LIMIT N, 1
  );
END

176. 第二高的薪水

思路1: row_number

select (select salary 
from 
(select salary, row_number() over (order by salary desc) as rn
from (
select DISTINCT salary from Employee) as r
) as t 
where rn = 2 ) as SecondHighestSalary

思路2:limit offset

select (select DISTINCT salary
from Employee
order by salary DESC
limit 1 offset 1) as SecondHighestSalary;

思路3: 和思路2相同,只不过加了ifnull

select IFNULL((select DISTINCT salary
from Employee
order by salary DESC
limit 1 offset 1),NULL) as SecondHighestSalary;

175. 组合两个表

select firstName, lastName, 
city, state    
from 
Person as p 
left join 
Address as a 
on p.PersonId = a.PersonId
举报

相关推荐

0 条评论