LeetCode 高频 SQL 50 题(基础版) 之 【高级查询和连接】· 下
上部分链接:LeetCode 高频 SQL 50 题(基础版) 之 【高级查询和连接】· 上
题目:1164. 指定日期的产品价格
题解:
select product_id,10 price from Products
group by product_id
having min(change_date) > '2019-08-16'unionselect product_id,new_price price from Products
where (product_id,change_date) in (select product_id,max(change_date) from Productswhere change_date<='2019-08-16'group by product_id
)
题目:1204. 最后一个能进入巴士的人
题解:
select t1.person_name
from Queue t1,Queue t2
where t1.turn>=t2.turn
group by t1.person_id
having sum(t2.weight)<=1000
order by t1.turn desc
limit 1
select t.person_name from (select turn,person_name, sum(weight) over(order by turn) sum_weightfrom Queue
) t
where t.sum_weight<=1000
order by t.sum_weight desc
limit 1
题目:1907. 按分类统计薪水
题解:
select "Low Salary" category,count(*) accounts_count from Accounts
where income<20000
union
select "Average Salary" category,count(*) accounts_count from Accounts
where income>=20000 and income<=50000
union
select "High Salary" category,count(*) accounts_count from Accounts
where income>50000