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';