34 - 指定日期的产品价格(高频 SQL 50 题基础版)

34 - 指定日期的产品价格


sql 复制代码
-- row_number(行号) 生成连续的序号,不考虑分数相同
-- 在'2019-08-16'之前改的价格,使用最近一期的日期,没有在'2019-08-16'之前改的价格,默认价格为10
select 
    t.product_id, 
    t.new_price as price
from 
    (
        select 
            *,
            row_number() over (PARTITION BY product_id order by change_date desc) as row_num
        from 
            Products
        where 
            change_date<='2019-08-16') 
    as t
where 
    t.row_num=1
union
-- 没有在'2019-08-16'之前改的价格,默认价格为10
select 
    product_id, 10 as price 
from 
    Products 
group by 
    product_id
having 
    min(change_date)>'2019-08-16';
相关推荐
treesforest14 小时前
高精度IP定位怎么选?从企业需求出发的选型指南
网络·数据库·tcp/ip·web安全·ip·高精度ip查询
吴声子夜歌14 小时前
Redis 5.x——多种数据类型使用场景
数据库·redis
tant1an15 小时前
MySQL面试题(一)
数据库·mysql
喜欢的名字被抢了15 小时前
MongoDB生产实践:架构设计、分片集群与运维监控
数据库·mongodb·教程
红糖奶茶15 小时前
MySQL 8.0的自增主键持久化特性
数据库·mysql·adb
Macbethad16 小时前
基于TwinCAT的半导体刻蚀设备SCADA管理程序技术方案
运维·网络·数据库
ai_coder_ai16 小时前
编写自动化脚本,在自己后端服务中使用云原生Baas服务进行设备相关操作
数据库·云原生·自动化
oradh16 小时前
Oracle DG主备架构的基本维护操作总结
数据库·oracle·架构·dg主备架构的基本维护操作总结
xieliyu.17 小时前
MySQL 六大基础约束详解:NOT NULL/DEFAULT/UNIQUE/ 主键 / 外键 / CHECK
android·数据库·sql·mysql
Anokata17 小时前
MYSQL SQL 执行系列1
android·sql·mysql