![](https://img-blog.csdnimg.cn/20190927151043371.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
sql笔记
sql学习工作笔记,持续更新
安奇999
互联网数据分析师一枚
展开
-
hive-sql简单查询where筛选
where筛选使用where语句可以按条件筛选数据1 准备数据商品明细数据drop table if exists test.test_zw;CREATE TABLE if not exists test.test_zw( goods_name string COMMENT '商品名', brand_name string COMMENT '品牌名', spec string comment '规格', price double comment '价格')COMMENT '测原创 2021-07-07 11:42:55 · 2930 阅读 · 0 评论 -
hive-sql简单查询之select语句
文章目录1 准备数据2 取出所有行和列3 限制返回行数4 选取指定的列5 重命名列6 单行注释7 distinct 去重8 group by 去重select语句是最基本最常用的sql语句,也非常简单。1 准备数据运行下面代码,在test数据库,新建一个test_zw表,并插入几条测试数据。drop table if exists test.test_zw;CREATE TABLE if not exists test.test_zw( name string COMMENT '姓名',原创 2021-07-06 10:56:26 · 1832 阅读 · 0 评论 -
hive窗口函数之排名函数row_number、rank和dense_rank
排名函数排名函数主要有row_number、rank和dense_rank。简单排名row_number是行号,不会重复,rank数据相同的,给出并列排名,但是会跳跃,dense_rank类似于rank,但不会跳跃,通过下面的案例来看一下区别。准备数据drop table if exists test.test_zw;CREATE TABLE if not exists test.test_zw( name string COMMENT '姓名', score double commen原创 2021-07-05 15:21:02 · 12310 阅读 · 0 评论 -
sql使用orderby排序
order by排序使用order by语句可以对返回的数据进行排序。准备数据drop table if exists test.test_zw;CREATE TABLE if not exists test.test_zw( name string COMMENT '姓名', course string comment '课程', score double comment '成绩')COMMENT '测试表'STORED as parquet TBLPROPERTIES('pa原创 2021-07-05 13:19:32 · 789 阅读 · 3 评论 -
hive 正则表达式-regexp
regexp在hive和mysql中都可以使用,匹配商品名称中包含伊利的数据goods_name regexp '伊利' 等价于goods_name like '%伊利%'看起来也没啥,但是当后面的关键字是变量时,优势就很明显了。比如在BI系统中,使用goodsnamegoods_namegoodsname,可以接收用户输入的关键字进行模糊查询。正表达式的写法如下,like的写法就要想办法处理百分号的问题。goods_name regexp $goods_name$ ...原创 2021-07-02 15:22:50 · 2390 阅读 · 0 评论 -
hive底表的坑-依赖链太长了
业务同学想要看虚假发货的定义,之前的文档找不到了,于是查看代码是如何判断的。判断逻辑来自表1的是否虚假发货字段。然后开始溯源,一不小心竟然查了十级。过程如下:1、是否虚假发货字段来自表1.2、表1的字段来自表2.3、表2的字段来自表34、表3的字段来自表4。5、表4的字段来自表5。6、表5的字段来自表6。7、表6的字段来自表7。8、表7的字段来自表8。9、表8的字段来自表9。10、表9的字段来自表10,终于找到你。这只是查个数据定义,一旦这个数据出问题,需要修复数据的话,呵呵,我太难了原创 2021-07-01 11:37:30 · 167 阅读 · 0 评论 -
hive和presto计算日期时间差
月份差:计算两个日期的月份差,下面代码只适用于hive,返回4select floor(months_between('2018-07-01','2018-02-04')) -- hive 日期差前面的日期减去后面的日期,返回-1,晚一天select datediff('2020-07-10','2020-07-11')小时差先转换为时间戳格式,计算秒级差,再换算成小时数select round((unix_timestamp('2021-07-01 11:10:10')-原创 2021-07-01 10:46:15 · 3467 阅读 · 0 评论 -
hive和presto时间戳处理
unix_timestamp返回当前时间的时间戳格式,整数类型,单位为秒1594695043select unix_timestamp() -- 返回当前时间的时间戳格式 1594695043 秒将指定日期转为时间戳,返回1577844548select unix_timestamp('2020-01-01 10:09:08') from_unixtime时间戳转日期select from_unixtime(1577844548)时间戳转日期,指定格式select from_un原创 2021-07-01 10:23:41 · 982 阅读 · 0 评论 -
hive和presto处理月末月初
月份select substr(settle_date,1,7) 月初select trunc(date_add(current_date,-1),'MM') as month_first_day -- hive 月初select date_trunc('month',current_date) -- presto 月初select date_format('2020-01-02 10:09:08','yyyy-MM-01') -- hive-presto select trunc(原创 2021-07-01 10:15:55 · 3157 阅读 · 1 评论 -
hive日期时间获取星期几
使用下面的代码,返回星期几,适用于hive,不适用于presto。select case pmod(datediff('2021-06-30', '1920-01-01') - 3, 7) when 0 then '星期天' when 1 then '星期一' when 2 then '星期二' when 3 then '星期三'when 4 then '星期四' when 5 then '星期五' when 6 then '星期六' else '其它' end as week_day原创 2021-06-30 22:22:50 · 720 阅读 · 3 评论 -
hive日期时间常用函数
current_date返回当前日期select current_date -- 今天select date_add(current_date,1) -- 明天select date_add(current_date,-1) -- 昨天date_add和date_subdate_add,增加日期 ,负数减少日期select date_add('2020-01-01',1) -- 返回2020-01-02select date_add('2020-01-01',-1) -- 返回201原创 2021-06-30 22:18:41 · 395 阅读 · 0 评论 -
hive正则表达式
正则匹配使用regexp进行正则匹配准备数据drop table if exists test.test_zw;CREATE TABLE if not exists test.test_zw( stu_id int COMMENT '学生id', mobile string comment '手机号')COMMENT '测试表'STORED as parquet TBLPROPERTIES('parquet.compression'='SNAPPY'); insert in原创 2021-06-30 22:07:11 · 402 阅读 · 0 评论 -
presto和hive字符串常用函数
substr字符串截取从第二个字符开始,截到最后select substr('12345678',2) -- 返回 '2345678' 从第二个开始,截取3个字符select substr('12345678',2,3) -- 返回234concat 连接字符串可以连接不同类型返回abcd1234select concat('abcd',1234) 计算百分比select concat(round(gmv/total_gmv*2*100,2),'%') as `gmv占比`原创 2021-06-30 19:52:26 · 2589 阅读 · 0 评论 -
presto和hive处理json字符串数据
hive和presto可以使用get_json_object函数解析json字符串。简单json字符串准备数据drop table if exists test.test_zw;CREATE TABLE if not exists test.test_zw( stu_id int COMMENT 'id', info string comment '学生信息')COMMENT '测试表'STORED as parquet TBLPROPERTIES('parquet.compress原创 2021-06-30 17:28:26 · 3230 阅读 · 1 评论