力扣SQL50 指定日期的产品价格 双重子查询 coalesce

Problem: 1164. 指定日期的产品价格
在这里插入图片描述

  • coalesce 的使用
    coalesce

简洁版

👨‍🏫 参考题解

select 
    distinct p1.product_id,
    coalesce((select p2.new_price
    from Products p2
    where p2.product_id = p1.product_id and p2.change_date <= '2019-08-16'
    order by p2.change_date DESC
    limit 1),10) as price 
from Products p1

高效版

👨‍🏫 参考题解

select p1.product_id, ifnull(p2.new_price, 10) as price
from (
    select distinct product_id
    from products
) as p1 -- 所有的产品
left join (
    select product_id, new_price 
    from products
    where (product_id, change_date) in (
        select product_id, max(change_date)
        from products
        where change_date <= '2019-08-16'
        group by product_id
    )
) as p2 --2019-08-16 之前有过修改的产品和最新的价格
on p1.product_id = p2.product_id


相关推荐

  1. 【LeetCode】1164. 指定日期产品价格

    2024-07-21 06:12:02       53 阅读
  2. 560. 和为 K 数组

    2024-07-21 06:12:02       20 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 06:12:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 06:12:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 06:12:02       45 阅读
  4. Python语言-面向对象

    2024-07-21 06:12:02       55 阅读

热门阅读

  1. 【音视频】AAC编码器与ffmpeg生成AAC数据

    2024-07-21 06:12:02       16 阅读
  2. 求职学习day7

    2024-07-21 06:12:02       19 阅读
  3. 算法学习2——排序算法(2)

    2024-07-21 06:12:02       15 阅读
  4. Jupyter Notebook与机器学习:使用Scikit-Learn构建模型

    2024-07-21 06:12:02       19 阅读
  5. C#面:ASP.NET Core项目如何设置IP地址和端口号

    2024-07-21 06:12:02       14 阅读
  6. 有关css的题目

    2024-07-21 06:12:02       16 阅读
  7. Go: IM系统接入ws进行消息发送以及群聊功能 (5)

    2024-07-21 06:12:02       14 阅读
  8. Perl编程秘籍:匿名数组与哈希的隐秘力量

    2024-07-21 06:12:02       15 阅读