背景

在数据开发中,有些情况下,需要手动生成批量SQL,只需改变某个参数,比如日期,从某天到某天。

之前有一个实例,是用 stringr::str_replace_all() 去实现,这次就用 glue 来做示例,会更便捷。

glue

glue , 是 tidyverse 项目的一部分,擅长处理长字符串和文本段落,支持在字符串中使用变量和表达式,书写较为自由、灵活。

glue主页 https://glue.tidyverse.org/

目标

下面示例的表,是某WMS仓库每日库存快照。

在数据首次同步及初始化时,第一个日期分区包含了所有业务日期的数据。之后的每日分区中,只包含一天的业务数据。现需要将初始日期分区,改为独立的日期分区,使得每个日期分区只包含一天的业务数据。

SQL脚本中,${bizdate}是一个参数,批量生成的目标是改变参数,从 2020082620201125

备注:该问题可以通过动态分区一次性实现,这里用指定分区执行多次的笨方法。

insert overwrite table edw.dwd_wms_inv_snapshot_inventory_d partition (dt = '${bizdate}')SELECTid,warehouse_code,item_code,bar_code,lot_no,quality,company_code,qty,inventory_date,created_dtm_loc,updated_dtm_loc,etl_insert_timeFROM edw.dwd_wms_inv_snapshot_inventory_dWHERE dt = '20201125' and inventory_date = to_date('${bizdate}', 'yyyymmdd');

R语言实现

# 载入所需的R语言包library(magrittr)  # 使用 %>% 这个 pipeline library(lubridate) # 日期处理library(glue)      # 字符段落处理
# 生成日期向量,格式改为 `yyyymmdd` ,这是 dt 日期分区的目标格式ymd(20200826):ymd(20201125) %>%  as_date() %>%  as.character.Date(format = "%Y%m%d") ->  bizdate

# 检查是否符合预期print(bizdate)##  [1] "20200826" "20200827" "20200828" "20200829" "20200830" "20200831"##  [7] "20200901" "20200902" "20200903" "20200904" "20200905" "20200906"## [13] "20200907" "20200908" "20200909" "20200910" "20200911" "20200912"## [19] "20200913" "20200914" "20200915" "20200916" "20200917" "20200918"## [25] "20200919" "20200920" "20200921" "20200922" "20200923" "20200924"## [31] "20200925" "20200926" "20200927" "20200928" "20200929" "20200930"## [37] "20201001" "20201002" "20201003" "20201004" "20201005" "20201006"## [43] "20201007" "20201008" "20201009" "20201010" "20201011" "20201012"## [49] "20201013" "20201014" "20201015" "20201016" "20201017" "20201018"## [55] "20201019" "20201020" "20201021" "20201022" "20201023" "20201024"## [61] "20201025" "20201026" "20201027" "20201028" "20201029" "20201030"## [67] "20201031" "20201101" "20201102" "20201103" "20201104" "20201105"## [73] "20201106" "20201107" "20201108" "20201109" "20201110" "20201111"## [79] "20201112" "20201113" "20201114" "20201115" "20201116" "20201117"## [85] "20201118" "20201119" "20201120" "20201121" "20201122" "20201123"## [91] "20201124" "20201125"

# 验证有多少天(向量长度):总共有 92 天length(bizdate)## [1] 92
# 在 glue 字符串中,是可以直接使用变量的,用 {} 引用起来即可# 如果原字符串中包含{}符号,可以修改 glue(.open = "{",  .close = "}",) 中的参数改为其他符号glue("insert overwrite table akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df partition (dt = '{bizdate}')SELECTid,warehouse_code,item_code,bar_code,lot_no,quality,company_code,qty,inventory_date,created_dtm_loc,updated_dtm_loc,etl_insert_timeFROM akdc.ods_akc_wms_cloud_inv_snapshot_inventory_dfWHERE dt = '20201125' and inventory_date = to_date('{bizdate}', 'yyyymmdd');") ->  sql
# 因为 bizdate 是字符向量,故而 sql 在经过 glue() 函数中使用了 bizdate 计算之后# sql 也是向量, 其长度保持与 bizdate 一致,也是 92,不需要再用 for 显式循环length(sql)## [1] 92
# 查看前两个SQL语句head(sql, 2) %>%   cat(sep = "\n\n")## insert overwrite table akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df partition (dt = '20200826')## SELECT## id,## warehouse_code,## item_code,## bar_code,## lot_no,## quality,## company_code,## qty,## inventory_date,## created_dtm_loc,## updated_dtm_loc,## etl_insert_time## FROM akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df## WHERE dt = '20201125' ## and inventory_date = to_date('20200826', 'yyyymmdd')## ;## ## insert overwrite table akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df partition (dt = '20200827')## SELECT## id,## warehouse_code,## item_code,## bar_code,## lot_no,## quality,## company_code,## qty,## inventory_date,## created_dtm_loc,## updated_dtm_loc,## etl_insert_time## FROM akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df## WHERE dt = '20201125' ## and inventory_date = to_date('20200827', 'yyyymmdd')## ;
# 查看后两个SQL语句tail(sql, 2) %>%   cat(sep = "\n\n")## insert overwrite table akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df partition (dt = '20201124')## SELECT## id,## warehouse_code,## item_code,## bar_code,## lot_no,## quality,## company_code,## qty,## inventory_date,## created_dtm_loc,## updated_dtm_loc,## etl_insert_time## FROM akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df## WHERE dt = '20201125' ## and inventory_date = to_date('20201124', 'yyyymmdd')## ;## ## insert overwrite table akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df partition (dt = '20201125')## SELECT## id,## warehouse_code,## item_code,## bar_code,## lot_no,## quality,## company_code,## qty,## inventory_date,## created_dtm_loc,## updated_dtm_loc,## etl_insert_time## FROM akdc.ods_akc_wms_cloud_inv_snapshot_inventory_df## WHERE dt = '20201125' ## and inventory_date = to_date('20201125', 'yyyymmdd')## ;
# 将结果写入到文件中(控制台显示不下)sql %>%  write(file = "out_put.txt", sep = "\n\n")

# 打开该目录新生成的txt文件就可以看到全部,复制出来即可# 可直接用RStudio文件模块直接点击打开文本窗口,也可用记事本之类的软件打开

补充示例1

library(magrittr) # 使用 %>% 这个 pipeline library(glue)     # 字符段落处理

# 生成日期序列,并转为字符型,格式为 yyyy-mm-ddseq(from = as.Date("2018-06-11"),     to = as.Date("2018-06-13"),     by = 1    ) %>%  as.character() ->  seq_date

glue("insert overwrite table rpt.rpt_collection_appoint_stronger_in_daily partition (dt = '{seq_date}')select appoint_stronger, count(case_id) as case_numfrom edw.collection_case_strength_hwhere to_date(start_time) <= '{seq_date}'and to_date(end_time) >= '{seq_date}'group by appoint_stronger;") %>%  cat(sep = "\n\n") # 输出大屏幕,在结尾换行,之后再换行另起一行## insert overwrite table rpt.rpt_collection_appoint_stronger_in_daily partition (dt = '2018-06-11')## select appoint_stronger, count(case_id) as case_num## from edw.collection_case_strength_h## where to_date(start_time) <= '2018-06-11'## and to_date(end_time) >= '2018-06-11'## group by appoint_stronger## ;## ## insert overwrite table rpt.rpt_collection_appoint_stronger_in_daily partition (dt = '2018-06-12')## select appoint_stronger, count(case_id) as case_num## from edw.collection_case_strength_h## where to_date(start_time) <= '2018-06-12'## and to_date(end_time) >= '2018-06-12'## group by appoint_stronger## ;## ## insert overwrite table rpt.rpt_collection_appoint_stronger_in_daily partition (dt = '2018-06-13')## select appoint_stronger, count(case_id) as case_num## from edw.collection_case_strength_h## where to_date(start_time) <= '2018-06-13'## and to_date(end_time) >= '2018-06-13'## group by appoint_stronger## ;

补充示例2

library(magrittr) # 使用 %>% 这个 pipeline library(glue)     # 字符段落处理

seq.Date(from = as.Date("2018-05-24"),          to = as.Date("2018-06-02"),          by = 1         ) %>%  as.character() ->   seq_date

# glue 支持在行尾使用\\双反斜杠来断行,而不会造成原字符串换行# 在代码书写尽可能避免水平滚动条的出现是基本原则,该该功能非常实用glue("alter table \\rpt.rpt_collection_ccms_oacase_list \\drop if exists partition \\(dt = '{seq_date}');")## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-24');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-25');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-26');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-27');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-28');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-29');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-30');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-05-31');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-06-01');## alter table rpt.rpt_collection_ccms_oacase_list drop if exists partition (dt = '2018-06-02');

sql 关联使用id还是code_R语言实例:用glue批量生成SQL语句相关推荐

  1. MySQL 批量生成 SQL 脚本语句解决实际的业务需求/如何拼接字符串/拼接字符串的 SQL 语句

    文章目录 实际需求 分析思路 写拼接 SQL 脚本的脚本语句 执行得到脚本语句 保存成 SQL 脚本文件 实际需求 有些行政区域的字段 area_fullname 是空的,如何补全呢?如下所示: 分析 ...

  2. Excel 数据批量生成SQL语句

    假设excel表格中有三列(A.B.C)数据,我们希望可以利用这三列数据批量生成SQL语句 第一步:新增D列,在D1中输入公式:=CONCATENATE("insert into user  ...

  3. 如何使用excel批量生成sql语句

    使用excel批量生成sql语句 1.将sql数据导出到excel文件 2.去除execl中多余的空格 设置单元格格式(如果不定义数据格式,去除数据前的空格后数据前的00会消失,如"001& ...

  4. php 自动生成sql,用PHP批量生成SQL更新语句 网页版

    有时候我们遇到很的SQL需要更新的时候,主意是大批量的时候 如果手动去添加单引号 双引号啥的 效率是非常的慢的! 下面看看批量成成的结果图: 如果是需要手动输入的是不是很慢呢! 代码逻辑思路很简单, ...

  5. 使用Excel批量生成SQL语句,用过的人都说好

    点击关注公众号,SQL干货及时获取 后台回复:1024,获取海量学习资源 Excel的公式自动生成想必大家都知道了,就是写好一个公式后直接往下拖,就可以将后面数据的公式自动生成. 今天我们就用这个功能 ...

  6. excel如何生成mysql的sql语句_excel 批量生成SQL语句

    文章来源:https://www.cnblogs.com/allen0118/p/3726455.html 我们经常会遇到这样的要求:用户给发过来一些数据,要我们直接给存放到数据库里面,有的是Inse ...

  7. R语言eval,parse批量生成变量并赋值

    R语言程序有时需要根据一定的规律批量生成变量,并赋值,如果手动一个个写出变量名并赋值太麻烦.可以通过eval结合parse函数批量操作. ### 把1:10 分别赋给10个变量,变量名按规律生成 fo ...

  8. sql 关联使用id还是code_使用sh格式化nginx访问日志并存入mysql

    概述 说明:记录的日志格式(字段有:ip,time,method,uri,http,code,datasize,head,postdata) 步骤: 1.设置nginx日志格式 2.使用sh脚本格式化 ...

  9. 【Python实例分析】批量生成海报--自动添加姓名和二维码

    最近参加了老男孩的一个python训练营,里面某项任务是要求在某个海报模板上批量添加姓名和二维码,生成类似下图的海报. 图中我用红色方框标记的是需要修改的地方,先来聊下自己的思路: 1.要进行图片操作 ...

最新文章

  1. 新型超低功耗无线网卡诞生,一颗纽扣电池可撑数年
  2. cc2530期末试卷_ZigBee应用技术答案试题题目及答案,期末考试题库,章节测验答案...
  3. python 面试题 博客园_python面试题
  4. skyline TerraBuilder 制作MPT方法与技巧(2)(转自)
  5. Docker系列06—基于容器制作镜像并上传到Docker Registry
  6. win7内部版本7601副本不是正版
  7. 太原理工大学计算机宿舍,2019太原理工大学宿舍怎么样 环境好不好
  8. 1090. Highest Price in Supply Chain (25)
  9. Lecture_2_4 线性回归中的系数,衡量了什么?
  10. CMake快速入门01:CMake简介与安装
  11. 51nod-1562:玻璃切割(O(n)模拟)
  12. 如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面(二)
  13. pano2vr无法输出html5,教大家Pano2VR怎么输出全景图的方法
  14. win7需要计算机管理员权限,解决方案:Win7安装软件需要管理员权限解决方案
  15. c++对象模型探索-王健伟-专题视频课程
  16. 如何判断一个点在三角形内部
  17. 多种好看好玩的词云例子Example
  18. lnmp下nginx出现5xx问题解决汇总
  19. 【Deep Learning学习笔记】Deep learning for nlp without magic_Bengio_ppt_acl2012
  20. 《计算机组成原理》复习第五章—中央处理器

热门文章

  1. python cmath模块_cmath模块-PYTHON
  2. vs2003 局部友元访问私有不可访问_C++ 类:重载运算符与友元
  3. 关于Python3.7和Python3.6中元组类型数据内存存储问题
  4. python中的元组操作
  5. 【机器学习】XGBoost集成算法——(理论+图解+python代码比较其他算法使用天池蒸汽数据)
  6. 【python】urllib和urllib3,requests 简要概括---笔记
  7. 【python】hashlib.shasha256练习注册 --笔记
  8. 服务器系统server 2008,windows server 2008 R2 操作系统
  9. 第一季3:HI3518E方案整体架构介绍(硬件和软件支持)
  10. nanotime java 博客园_System.nanoTime (计时工具类)