标签:分时 图片 dia orm 生成 数据校验 period from 原因
问题:如何将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
注意,理论上结果中同一行两个时间只相差一年
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,platform,period,dense_rank() over (partition by brand,platform order by period ) as rn2
from social_kol_tmp
where period>=‘201801‘ and period<‘201809‘
) b
on a.rn1=b.rn2 and a.brand=b.brand and a.platform=b.platform
注意,理论上结果中同一行两个时间只相差一年
某个品牌的某一平台的数据不是每个月都有,排序产生的字段rn1和rn2出了问题,未按照对应的月份排序。
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,
case when period=‘201901‘ then ‘1‘
when period=‘201902‘ then ‘2‘
when period=‘201903‘ then ‘3‘
when period=‘201904‘ then ‘4‘
when period=‘201905‘ then ‘5‘
else period
end as rn1
from social_kol_tmp
where period>=‘201901‘ and period<‘201909‘
) a
join
( select col_cost,brand,platform,period,
case when period=‘201801‘ then ‘1‘
when period=‘201802‘ then ‘2‘
when period=‘201803‘ then ‘3‘
when period=‘201804‘ then ‘4‘
when period=‘201805‘ then ‘5‘
else period
end as rn2
from social_kol_tmp
where period>=‘201801‘ and period<‘201809‘
) b
on a.rn1=b.rn2 and a.brand=b.brand and a.platform=b.platform
检验:
Create table sMedia.social_kol_tmp_new as
select a.brand,a.platform,a.period as Adt,b.period as Bdt,a.kol_cost,b.kol_cost as cost_YNew
from (select kol_cost,brand,platform,period,
case when period=‘201901‘ then ‘1‘
when period=‘201902‘ then ‘2‘
when period=‘201903‘ then ‘3‘
when period=‘201904‘ then ‘4‘
when period=‘201905‘ then ‘5‘
when period=‘201906‘ then ‘3‘
when period=‘201907‘ then ‘4‘
when period=‘201908‘ then ‘5‘
when period=‘201909‘ then ‘5‘
else period
end as rn1
from Media.social_kol_tmp_new
where period>=‘201901‘ and period<‘201909‘
) a
join
( select kol_cost,brand,platform,period,
case when period=‘201801‘ then ‘1‘
when period=‘201802‘ then ‘2‘
when period=‘201803‘ then ‘3‘
when period=‘201804‘ then ‘4‘
when period=‘201805‘ then ‘5‘
when period=‘201806‘ then ‘6‘
when period=‘201807‘ then ‘7‘
when period=‘201808‘ then ‘8‘
when period=‘201809‘ then ‘9‘
else period
end as rn2
from Media.social_kol_tmp_new
where period>=‘201801‘ and period<‘201809‘
) b
on a.rn1=b.rn2 and a.brand=b.brand and a.platform=b.platform
update sMedia.social_kol_tmp
set cost_YA= cost_YNew
from sMedia.social_kol_tmp a,Media.social_kol_tmp_new b
where a.period=b.Adt
标签:分时 图片 dia orm 生成 数据校验 period from 原因
原文地址:https://www.cnblogs.com/sonia0087/p/11728133.html