【高频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

相关推荐
倔强的石头_1 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
云技纵横1 天前
唯一索引 INSERT 死锁实战:5 秒复现交叉插入的 S 锁循环等待
sql·mysql
冬奇Lab2 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence2 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神2 天前
三、用户与权限管理
数据库·mysql
麦聪聊数据3 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡3 天前
【MySQL数据库】数据类型与表约束
数据库·mysql
曹牧3 天前
Oracle EXPLAIN PLAN
数据库·oracle
BD_Marathon3 天前
SQL学习指南——视图
数据库·sql