案例:利用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,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

2. 出现的问题:部分时间不对应

注意,理论上结果中同一行两个时间只相差一年

3 .原因分析:

某个品牌的某一平台的数据不是每个月都有,排序产生的字段rn1和rn2出了问题,未按照对应的月份排序。

方案二:case when 生成rn1rn2(成功)

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,

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

2.结果如下:

检验:

  • Adt和Bdt的日期月份是对应的
  • 表中的数据Adt对应的col_cost是以1开头的,Bd对应的是以2开头的,故数据校验成功

sql中实现数据更新的方法

参考如下(sqlsever):

1.创建一个表,sMedia.social_kol_tmp_new 201901-201909中的kol_cost替换为对应18年月份的数据

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

 2.sMedia.social_kol_tmp 更新新数据

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

原文地址:https://www.cnblogs.com/sonia0087/p/11728133.html

时间: 2024-08-01 01:19:14

案例:利用sql批量更新部分时间的同比数据的相关文章

SqlServer 利用游标批量更新数据

原文:SqlServer 利用游标批量更新数据 SqlServer 利用游标批量更新数据 Intro# 游标在有时候会很有用,在更新一部分不多的数据时,可以很方便的更新数据,不需要再写一个小工具来做了,直接写 SQL 就可以了 Sample# 下面来看一个实际示例: Copy -- 声明字段变量 DECLARE @RegionCode INT; DECLARE @RegionName NVARCHAR(64); DECLARE @ProvinceId INT; -- 声明游标 DECLARE P

SQL批量更新具有树形结构表Path字段

如上图所示,需要更新该表中Path字段,如ID=14的Path值:-1,ID=17的Path值:-1.14,ID=20的Path值:-1.14.18.19 步骤1.创建函数 Create FUNCTION [dbo].[F_Org] ( @id int ) RETURNS TABLE AS RETURN ( with testTable as( select UpperID from Core_DeptInfo where ID=@id union all select Core_DeptInf

sql server 更新表,每天的数据分固定批次设置批次号sql

按表中的字段 UpdateTime 按每天进行编号,每天的编号都从1开始编号,并附带表的主键 cid,把数据存入临时表中 WITH temp AS (SELECT cid,updatetime, ROW_NUMBER() OVER (PARTITION BY CONVERT(varchar(10), UpdateTime, 20) ORDER BY updatetime) AS Row_Num FROM Customer_Transfer ) 从临时表中根据主键cid进行更新 BatchNo,给

Oracle两张表关联批量更新其中一张表的数据

Oracle两张表关联批量更新其中一张表的数据 方法一(推荐): UPDATE 表2 SET 表2.C = (SELECT B FROM 表1 WHERE 表1.A = 表2.A) WHERE EXISTS (SELECT 1 FROM 表1 WHERE 表1.A = 表2.A); 尤其注意最后的外层where条件尤为重要,是锁定其批量更新数据的范围. 方法二: MERGE INTO 表2 USING 表1 ON (表2.A = 表1.A) -- 条件是 A 相同 WHEN MATCHED TH

MySQL批量更新不同表中的数据

今天翻到以前写的批量更新表中的数据的存储过程,故在此做一下记录. 当时MySQL中的表名具有如下特征,即根据需求将业务表类型分为了公有.私有和临时三种类型,即不同的业务对应三张表,而所做的是区分出是什么类型(公有.私有.临时)的业务表对数据的固定字段做统一规律的处理. 下面为当时所编写的存储过程: BEGIN DECLARE done INT; DECLARE v_table_name VARCHAR(100); DECLARE v_disable VARCHAR(100); DECLARE v

MongoDB批量更新不同查询条件的数据

今天遇到这样一个场景:在Java中批量更新MongoDB数据,不过每次更新的条件有不一样,那如何有效地进行更新操作呢? 刚开始的时候,我是想到循环批量更新操作,即每一种查询条件进行一次批量更新过程,这样的思路就是多个查询条件就需要进行多次的批量更新,带来的弊端是跟数据库的连接交互太频繁了,消耗的时间都浪费在这些过程中了:那么今天我们可以通过一种新的思路来避免这种负面影响,即只需要两次连接交互就可以了. ------20200113勉 原文地址:https://www.cnblogs.com/bi

SQL数据同步到ELK(四)- 利用SQL SERVER Track Data相关功能同步数据(上)

一.相关文档 老规矩,为了避免我的解释误导大家,请大家务必通过官网了解一波SQL SERVER的相关功能. 文档地址: 整体介绍文档:https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/about-change-tracking-sql-server?view=sql-server-2017 Change Data Capture:https://docs.microsoft.com/en-us/sql/

C#批量更新mongodb符合条件的数据

默认情况下只会更新匹配的第一条 jingjiaanalyurl.Update(Query.EQ("auid", jingjiaitem.id), Update.Set("aurlname", jingjiaitem.urlname), UpdateFlags.Multi);

SQL中更新A表的字段数据到B表的字段

update t_test1 set name = (select name from t_test2 b where t_test1.id = b.id)