【高频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 小时前
MongoDB Windows 系统实战手册:从配置到数据处理入门
数据库·mongodb
QuZhengRong2 小时前
【数据库】Navicat 导入 Excel 数据乱码问题的解决方法
android·数据库·excel
码农阿豪2 小时前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
时序数据说7 小时前
时序数据库市场前景分析
大数据·数据库·物联网·开源·时序数据库
听雪楼主.11 小时前
Oracle Undo Tablespace 使用率暴涨案例分析
数据库·oracle·架构
我科绝伦(Huanhuan Zhou)11 小时前
KINGBASE集群日常维护管理命令总结
数据库·database
妖灵翎幺11 小时前
Java应届生求职八股(2)---Mysql篇
数据库·mysql
HMBBLOVEPDX11 小时前
MySQL的事务日志:
数据库·mysql
YA33313 小时前
java基础(九)sql基础及索引
java·开发语言·sql
weixin_4196583113 小时前
MySQL数据库备份与恢复
数据库·mysql