SQL价格变动会后的最新价格

商品信息表: sku_info 

字段:sku_id(商品ID) name(商品名称) from_date(上架日期) price(商品价格)

价格变动表 :sku_price_modify_date
字段:sku_id 商品ID)new_price(商品变动后新价格) change_date(变动日期)

需求:查询所有商品表 sku_info 截止到21年10月1日最新价格



 

 1 select 
 2  a.sku_id,cast(nvl(c.new_price,a.price),decimal(16,2)) price 
 3  
 4  from (
 5 
 6 select sku_id,price from sku_info
 7 ) a left join (
 8 
 9     select sku_id,new_price from (
10         select sku_id,new_price,change_date,rwo_number() over(partition by sku_id order by change_date desc ) dr from sku_price_modify_date
11         where change_date <= '2021-10-01'
12     ) b where b.dr = 1
13 
14 ) c on a.sku_id = c.sku_id

 

posted on 2025-06-21 17:30  北京的小乔  阅读(16)  评论(0)    收藏  举报