SQL同期销量

:order_detail

字段:order_id sku_id create_date sku_num

需求:同一商品在21年和22年中同一个月份的销量
期望结果:
sku_id month 2021年 2022年

 1 select 
 2  
 3  sku_id
 4  ,mont
 5  ,sum(case when years = '2021' then ct else 0 end )  2021年
 6  ,sum(case when years = '2022' then ct else 0 end )  2022年
 7  
 8  
 9  from (
10     select 
11      sku_id
12      ,substr(months,1,4) years
13      ,substr(months,6,2) mont
14      ,ct
15      
16      from(
17         select  sku_id,substr(create_date,1,7) months sum(sku_num) ct from order_detail
18         group by  sku_id,substr(create_date,1,7) 
19     ) a 
20 ) b group by  sku_id,montv

 

posted on 2025-06-23 12:12  北京的小乔  阅读(12)  评论(0)    收藏  举报