题目
产品数据表: Products
写一段 SQL来查找在 2019-08-16 时全部产品的价格,假设所有产品在修改前的价格都是 10 。
以 任意顺序 返回结果表。
查询结果格式如下例所示。
示例 1:
解题思路
代码实现
(select product_id , 10 as price
from Products
group by product_id
having min(change_date)>'2019-08-16')
union
(select product_id, new_price as price
from Products
where(product_id, change_date) in
(select product_id, max(change_date) as change_date
from Products
where change_date <= '2019-08-16'
group by product_id))