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';
相关推荐
daad77734 分钟前
wifi_note
运维·服务器·数据库
xixingzhe21 小时前
Mysql统计空间增量
数据库·mysql
程序员萌萌2 小时前
Redis的缓存机制和淘汰策略详解
数据库·redis·缓存机制·淘汰策略
不剪发的Tony老师2 小时前
SQLite 3.53.0版本发布,重要更新
数据库·sqlite
Bczheng12 小时前
九.Berkeley DB数据库 序列化和钱包管理(1)
数据库
cozil2 小时前
记录mysql创建数据库未指定字符集引发的问题及解决方法
数据库·mysql
架构师老Y2 小时前
013、数据库性能优化:索引、查询与连接池
数据库·python·oracle·性能优化·架构
AC赳赳老秦2 小时前
OpenClaw数据库高效操作指南:MySQL/PostgreSQL批量处理与数据迁移实战
大数据·数据库·mysql·elasticsearch·postgresql·deepseek·openclaw
一 乐2 小时前
校园线上招聘|基于springboot + vue校园线上招聘系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园线上招聘系统
liliangcsdn2 小时前
如何基于sentence_transformers构建向量计算工具
数据库·人工智能·全文检索