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';
相关推荐
weelinking1 天前
【产品】00_产品经理用Claude实现产品系列介绍
数据库·人工智能·sql·数据挖掘·github·产品经理
2301_803934611 天前
Go语言如何做网络爬虫_Go语言爬虫开发教程【指南】
jvm·数据库·python
秋91 天前
windows中安装redis
数据库·redis·缓存
Cosolar1 天前
万字详解:RAG 向量索引算法与向量数据库架构及实战
数据库·人工智能·算法·数据库架构·milvus
想唱rap1 天前
IO多路转接之poll
服务器·开发语言·数据库·c++
SeaTunnel1 天前
AI 让 SeaTunnel 读源码和调试过时了吗?
大数据·数据库·人工智能·apache·seatunnel·数据同步
凯瑟琳.奥古斯特1 天前
数据冗余与规范化的本质[数据库原理]
开发语言·数据库·职场和发展
_ku_ku_1 天前
数据库系统原理 · SQL 数据定义、更新及数据库编程 · 自学总结
数据库·oracle
Mortalbreeze1 天前
深度理解文件系统 ---- 从磁盘存储到内核存储
大数据·linux·数据库
2301_803934611 天前
MySQL 字段类型选择规范指南
jvm·数据库·python