当前位置: 首页 > news >正文

高频SQL50题 第九天 | 1164. 指定日期的产品价格、1204. 最后一个能进入巴士的人、1907. 按分类统计薪水

1164. 指定日期的产品价格

题目链接:https://leetcode.cn/problems/product-price-at-a-given-date/description/?envType=study-plan-v2&envId=sql-free-50
状态:已完成

考点

  • group by + select语句中使用聚合函数max():获取每个产品的最新日期
  • 子查询:获取2019-08-16之前每个产品的最新价格
  • 连接:针对未在2019-08-16之前更新价格的产品,通过连接操作产生的空值,结合ifnull()函数转化为默认值
select T1.product_id, ifnull(new_price, 10) as price
from (select distinct product_idfrom Products
)as T1 left join (select product_id, new_pricefrom Productswhere (product_id, change_date) in (select product_id, max(change_date) as max_datefrom Productswhere datediff(change_date, "2019-08-16") <= 0group by product_id)
) as T2
on T1.product_id = T2.product_id

1204. 最后一个能进入巴士的人

题目链接:https://leetcode.cn/problems/last-person-to-fit-in-the-bus/description/?envType=study-plan-v2&envId=sql-free-50
状态:需二刷,完全没有解题思路

解题思路:首先通过连接操作获取{1,2,3,…,i}的子集,然后计算每个子集的和,筛选出所有和<1000的子集,选取其中最大的i,即为最后一个进入巴士的人
考点

  • join + on,连接操作
  • group by + having,分组操作
  • order by + desc + limit k,降序排序+输出Top-k操作
select Q1.person_name
from Queue as Q1 join Queue as Q2
on Q1.turn >= Q2.turn
group by Q1.turn
having sum(Q2.weight) <= 1000
order by sum(Q2.weight) desc
limit 1

1907. 按分类统计薪水

题目链接:https://leetcode.cn/problems/count-salary-categories/?envType=study-plan-v2&envId=sql-free-50
状态:已完成

考点:使用UNION横向连接表格
:不需要手动创建临时表,直接使用UNION连接多个表格即可,MYSQL会自动创建临时表

select "Low Salary" as category, count(*) as accounts_count
from Accounts
where income < 20000
UNION
select "Average Salary" as category, count(*) as accounts_count
from Accounts
where income >= 20000 and income <= 50000
UNION
select "High Salary" as category, count(*) as accounts_count
from Accounts
where income > 50000

相关文章:

  • pytorch--模型训练的一般流程
  • 1 Studying《Computer Vision: Algorithms and Applications 2nd Edition》11-15
  • MySQL之全场景常用工具链
  • MYSQL与PostgreSQL的差异
  • (Arxiv-2025)Qwen2.5-VL 技术报告
  • mybatis-plus从入门到入土(一):快速开始
  • Embedding模型微调实战(ms-swift框架)
  • 医疗AI智能基础设施构建:向量数据库矩阵化建设流程分析
  • 领域驱动设计(DDD)【28】之实践或推广DDD的学习
  • 左神算法之矩阵旋转90度
  • <STC32G12K128入门第二十二步>STC32G驱动DS18B20(含代码)
  • IDE/IoT/实践小熊派LiteOS工程配置、编译、烧录、调试(基于 bearpi-iot_std_liteos 源码)
  • 2025.1版本PyCharam找不到已存在的conda虚拟环境
  • 领域驱动设计(DDD)【27】之CQRS四个层面的策略
  • Ubuntu服务器(公网)- Ubuntu客户端(内网)的FRP内网穿透配置教程
  • Spring Cloud 服务追踪实战:使用 Zipkin 构建分布式链路追踪
  • Python爬虫:Requests与Beautiful Soup库详解
  • MATLAB变音系统设计:声音特征变换(男声、女声、童声互转)
  • Windows 环境下设置 RabbitMQ 的 consumer_timeout 参数
  • c# 在sql server 数据库中批插入数据