【高频SQL题目】再做一遍 1164.指定日期的产品价格

题目要求:

产品数据表: Products

复制代码
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| product_id    | int     |
| new_price     | int     |
| change_date   | date    |
+---------------+---------+
(product_id, change_date) 是此表的主键(具有唯一值的列组合)。
这张表的每一行分别记录了 某产品 在某个日期 更改后 的新价格。

编写一个解决方案,找出在 2019-08-16时全部产品的价格,假设所有产品在修改前的价格都是 10。

以 任意顺序返回结果表。

结果格式如下例所示。

示例 1:

复制代码
输入:
Products 表:
+------------+-----------+-------------+
| product_id | new_price | change_date |
+------------+-----------+-------------+
| 1          | 20        | 2019-08-14  |
| 2          | 50        | 2019-08-14  |
| 1          | 30        | 2019-08-15  |
| 1          | 35        | 2019-08-16  |
| 2          | 65        | 2019-08-17  |
| 3          | 20        | 2019-08-18  |
+------------+-----------+-------------+
输出:
+------------+-------+
| product_id | price |
+------------+-------+
| 2          | 50    |
| 1          | 35    |
| 3          | 10    |
+------------+-------+

解题思路:

限时返场,再做一遍。仍然遇到了好几个问题。

先观察:

product_id为1,在 2019-08-16刚好有数据

product_id 为2,在 2019-08-16没有数据,但是在2019-08-14 和2019-08-17号有数据。很显然要取2019-08-14这一天的价格。

product_id为3,在 2019-08-16没有数据,在它之前也没有数据。所以就得用10.(题目里这样要求的)

1、要找各自的最靠近那个日期的去比较。

怎么找,是这个sql 的最重要的部分

用row_number (),根据产品id分组,框选日期要小于等于那一天,同时根据change_date倒序排序,取第一个。

这样就可以找到最靠近目标日期的最新日期的数据。

2、 日期应该大于等于 那天,还是小于等于那天?

同上。

3、日期范围这块我不熟悉!那天没有数据的时候。

那天没有数据没关系,用ifnull(),没有数据就给10.(题目里这样要求的)

-- 错误的sql。(因为row_number没用对。没有用分组)

-- select distinct p1.product_id,ifnull(p2.new_price,10) as price from Products p1 left join

-- ( select t.product_id,t.new_price from (select product_id,new_price ,row_number()over (order by change_date desc ) as rn from Products

-- where change_date<= '2019-08-16' ) t where t.rn =1 ) p2 on p1.product_id = p2.product_id

-- 正确的sql

select distinct p1.product_id,ifnull(p2.new_price,10) as price from Products p1 left join

( select t.product_id,t.new_price from (select product_id,new_price ,row_number()over (PARTITION BY product_id order by change_date desc ) as rn from Products

where change_date<= '2019-08-16' ) t where t.rn =1 ) p2 on p1.product_id = p2.product_id

相关推荐
这个DBA有点耶4 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
DBA_G4 天前
从地面到云霄:GBase数据库在民航三大场景的落地实践
数据库
自由能燃气设备4 天前
商用全预混低氮冷凝锅炉免费方案vs付费方案对比+选型避坑指南
大数据·数据库·人工智能
科创致远4 天前
科创致远 ESOP 系统核心效能与实战价值展示
大数据·数据库·人工智能·精益工程
漂着的圆木4 天前
Agent 功能参与度:Copilot 怎么算
sql·数据分析·agent·githubcopilot·度量
2601_962218614 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法
张洛闻Eren4 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
于平安4 天前
MySQL-触发器
数据库·mysql
白远山4 天前
上海24小时自助健身房解决方案实战指南与经验分享
java·数据库·架构·需求分析
Omics Pro5 天前
斯坦福Nature+Science|广义虚拟细胞基础大模型
数据库·人工智能·算法·机器学习·自然语言处理