问题:如何将social_kol_tmp表 中的字段cost_YA中日期为201901-201909中的值替换为相同brand和pltform对应18年月份的col_cost字段的数据,其他日期的cost_YA值不变?

假设: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

探究分析---利用sql批量更新部分时间的同比数据的更多相关文章

  1. SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int

    --SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int --关键说明:--1.从系统表syscolumns中的查询所有xtype='48'的记录得到类型为[tinyint]的字段- ...

  2. SqlServer 利用游标批量更新数据

    SqlServer 利用游标批量更新数据 Intro 游标在有时候会很有用,在更新一部分不多的数据时,可以很方便的更新数据,不需要再写一个小工具来做了,直接写 SQL 就可以了 Sample 下面来看 ...

  3. 利用sql批量删除表,存储过程

    利用sql批量删除表,存储过程. 最近用godaddy的空间,由于系统里面的表多,一个个的删除很麻烦,就网上搜集了一下解决方法. 给大家分享一下: 1.批量删除存储过程 declare @procNa ...

  4. SQL批量更新数据

    SQL批量更新数据 step1:导入Excel数据, 具体见百度.注意点:一列中含有float型数据和文本数据的时候,导入要将Excel中的表格属性改成文本,或在数字项目前加个单引号.   step2 ...

  5. oracle 批量更新之将一个表的数据批量更新至另一个表

      oracle 批量更新之将一个表的数据批量更新至另一个表 CreationTime--2018年7月3日17点38分 Author:Marydon Oracle 将一个表的指定字段的值更新至另一个 ...

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

    Oracle两张表关联批量更新其中一张表的数据 方法一(推荐): UPDATE 表2 SET 表2.C = (SELECT B FROM 表1 WHERE 表1.A = 表2.A) WHERE EXI ...

  7. sql 批量更新

    利用update和select进行批量更新 UPDATE Table1 SET categoryId =(SELECT Id FROM Table2 WHERE Table1 .Id=Table2.c ...

  8. 利用pip批量更新python库

    如果python库比较旧,需要更新到最新版本,可以利用pip工具. DOS命令行下,输入pip -V查看pip版本,可以先把pip更新到新版本. 查看系统里过期的python库 pip list #列 ...

  9. SQL批量更新 关系表更新

    很多人在做数据的批量更新时..如果更新的内容是从其他表查出来的..很容易这么写.. UPDATE TABLE1 SET COLUMN1=(SELECT SUM(SOMETHING) FROM TABL ...

随机推荐

  1. requests---重定向

    通常我们抓包的过程中,都会看到302的状态码,那么这个过程发生了什么? 什么是重定向 就是通过各种方法将各种网络请求重新定个方向转到其它位置,本来应该从a出发到达b但是最终到达了c,这种场景就叫做重定 ...

  2. 05webpack-webpack-dev-server时时跟新-第2种方式

    <!--14 第一种方式 自动打开浏览器 端口号 指定托管的跟目录 启动热刷新 这种是在webpack.json中去配置的 直接在package中 写 将“script”:{ "dev ...

  3. Vue工程化入口文件main.js中Vue.config.productionTip = false含义

    阻止启动生产消息,常用作指令.通俗理解为消息提示的环境配置. 阻止启动生产消息 這又是什麽意思? 看下效果 (1)Vue.config.productionTip = false (2)Vue.con ...

  4. Appium左右、上下滑动(Java)

    网上很多文章都说用swipe来左右滑动,你把代码一贴,结果报错,看半天,原来是java-client中swipe早就被废除了!!!下面介绍一种Java写法来左右上下滑动: 首先,创建一个Swipe类 ...

  5. 攻防世界web-unserialize3

    漏洞编号CVE-2016-7124 详情  https://xz.aliyun.com/t/378 题目源码 class xctf{ '; public function __wakeup(){ ex ...

  6. VMWare虚拟机提示:锁定文件失败,打不开磁盘...模块"Disk"启动失败的解决办法

    我出现该问题的原因: 昨天电脑一下子卡死,于是我就重启了电脑,重启之后我没有打开VMware虚拟机,结果第二天一上班打开VMware就发现出现了“锁定文件失败,打不开磁盘......模块"D ...

  7. [C4] Andrew Ng - Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization

    About this Course This course will teach you the "magic" of getting deep learning to work ...

  8. 让人又爱又恨的this

    this是个神奇的东西, 既可以帮助我们把模拟的类实例化. 又可以在事件绑定里准确指向触发元素. 还可以帮助我们在对象方法中操作对象的其他属性或方法. 甚至可以在使用apply.call.bing.f ...

  9. MySQL学习笔记8——多表查询

    多表查询 多表查询 *合并结果集 *连接查询 *子查询 合并结果集 *要求被合并的表中,列的类型和列数相同(实际上是查询的结果集列类型和列数相同即可) *UNION,去除重复行 *UNION ALL, ...

  10. Centos7防火墙添加端口

    添加 firewall-cmd --zone=public --add-port=80/tcp --permanent   (--permanent永久生效,没有此参数重启后失效) 重新载入 fire ...