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

相关推荐
StarRocks_labs3 小时前
StarRocks Community Monthly Newsletter (Jun)
数据库·starrocks·数据湖·物化视图·存算分离
光电的一只菜鸡4 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
ob熔天使——武4 小时前
MySQL
数据库·mysql
小光学长5 小时前
基于vue框架的防疫物资仓库管理系统09y38(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
野生技术架构师9 小时前
MySQL数据实时同步到Elasticsearch的高效解决方案
数据库·mysql·elasticsearch
白仑色9 小时前
Oracle 高可用性与安全性
数据库·oracle·数据安全·goldengate·高可用架构
紫无之紫10 小时前
SQL性能调优经验总结
数据库·sql·性能调优
CZZDg10 小时前
Redis Sentinel哨兵集群
java·网络·数据库
__风__10 小时前
PostgreSQL ExecInitIndexScan 函数解析
数据库·postgresql
小云数据库服务专线10 小时前
GaussDB in的用法
数据库·sql·gaussdb