案例:利用sql批量更新部分时间的同比数据
问题: 如何将social_kol_tmp表 中的字段cost_YA中日期为201901-201909中的值替换为相同brand和pltform对应18年月份的col_cost字段的数据,其他日期的cost_Y值不变? 假设: social_kol_tmp表 A,social_kol_tmp表B 难点: 可以利用join on brand和pltform相等,但是日期如何匹配呢? 思路: 通过对18年各月和对应19年的各个月份产生相应的字段,rn1和rn2 注意,理论上结果中同一行两个时间只相差一年 方案一:窗口函数生成rn1和rn2(结果需要看数据情况,原因如下) 1. 代码如下: select a.brand,a.platform,a.period as Adt,b.period as Bdt,a.col_cost,b.col_cost as cost_YNew from (select col_cost,brand,platform,period, dense_rank() over (partition by brand,platform order by period) as rn1 from social_kol_tmp where period>='201901' and period<'201909' ) a join ( select col_cost,brand