欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > hive SQL

hive SQL

2024/10/24 1:56:45 来源:https://blog.csdn.net/u010924845/article/details/140262940  浏览:    关键词:hive SQL
hivesql
一.实际生产常用hsql语句函数。
1.sysdate(-1) 因为hive 中表数据都是分区数据,所以一般都是取一定时间得数据所以这个必须知道自己所在公司用啥函数。这个函数些公司再hive中自己封装分区获取函数。(某东就是用sysdate(-1),代表当前减去1天)select * from app.app_zhonghuadadi where dt = sysdate(-1) --dt就是分区字段。
2.开窗函数,实际生产中就开创函数遍布每天得工作。select sum() over(partition by cate_id ) from app.app_zhonghuadadi where dt = sysdate(-1)--本代码,计算出app.app_zhonghuadadi表中分区是sysdate(-1)中数据,但是数据是经过开窗函数过滤后得。最后结果数据是每个品类下数据得条数。和group by 类似。一:开窗函数中的区间partition by cate_id order by desc rows between A and BA:  unbounded preceding 从头开始3 preceding 不算当前的前三行current row 当前行B:  current row 当前行3 following 不算当前行,往后的三行unbounded following 最本组的最后一行二:开窗函数select  max(case when score_name = 'yuwen' then score else 0 end) as yuwen_max,max(case when score_name = 'shuxue' then score else 0 end) as shuxue_maxfrom app.score group by student -- 每个学生语文的最高成绩和数学的最高成绩
3.开窗函数,实际生产中就开创函数遍布每天得工作。select row_number() over(partition by cate_id order by createtime) from app.app_zhonghuadadi where dt = sysdate(-1)--本代码,计算出app.app_zhonghuadadi表中分区是sysdate(-1)中数据,但是数据是经过开窗函数过滤后得。--最后结果是每个cate_id(分类)后。数据按照createtime(创建时间)来排序展示数据。--同理参考另外2个排序函数rank()、dense_rank()--同理真是生产中分组取topN得代码也是在这个得基础上整理出来得。因为有排名了。直接指定取多少名就ok了。
4.连接函数,left join 重中之重。为了性能考虑,一定要把小表放在 left join 左边
select a.*,b.xxx,b.sss from(select * from app.app_zhonghuadadi where sysdate(-1)) aleft join(select * from app.app_zhonghua where sysdate(-1)) bon a.sku = b.sku--一定记住left join 最后的展示得数据是左边表的数据全部展示在结果中,如果只想展示两个表都有的数据就不用left join 用下面的语句
select a.*,b.xxx,b.sss from(select * from app.app_zhonghuadadi where sysdate(-1)) a,(select * from app.app_zhonghua where sysdate(-1)) bwhere a.sku = b.sku--只展示两个表中都存在的数据
5.大小写转换和指定字符替换。lower(regexp_replace(v_key_new,' ','')))select regexp_replace('Big D a ta',' ','') --意思就是把'Big D a ta'中得空格去掉。此函数就是需要输入三个参数,第一个时字段,--第二个时需要替换得东西,最后是需要替换成得东西。select lower('Big D a ta') --大写变成小写select upper('Big D a ta') --小写变成大写
6.字符串得拼接语法select CONCAT('Big', 'Data') --计算结果就是BigData    
7.jason数据解析,这部分属于数据加工了。因为大数据得得数据有部分时埋点数据过来得。上游数据加工者可能并没有把所有你所需要得信息都加工出来,但是一般会有一个扩展字段,里面存有得时原始得json数据,这时候可以自己加工。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com