SQL
leewe7
这个作者很懒,什么都没留下…
展开
-
SQL case when 的用法
SQL case when 运用的场景原创 2022-11-10 11:32:11 · 769 阅读 · 0 评论 -
HIVE SQL 按照某字段先分区,分区内部排序,再取内部的第一条数据
with tmp as (select *,ROW_NUMBER() OVER (PARTITION BY name ORDER BY level1,level2) as num from table )select * from tmp where num=1原创 2022-02-24 20:03:52 · 1247 阅读 · 0 评论 -
如何在SQL中将某个算出来的值平摊到每一行中使用
构造一个关联字段,使用left join,例如求出总和,再求占比:with a as (select name,count(1) as c,1 as j from t1 group by name),b as (select count(1) as c,1 as j from t1)select a.name,a.c as cn,b.c as total,(100*a.c/b.c) from a left join b on a.j=b.j...原创 2021-07-12 20:08:10 · 620 阅读 · 0 评论 -
SQL case when 结合group by的应用方式
WITH c_temp AS (SELECT avg(CASE WHEN d<'20201102' THEN cnt ELSE 0 END) AS avg_7, sum(CASE WHEN d='20201102' THEN cnt ELSE 0 END) AS pv,原创 2020-11-03 18:58:40 · 1500 阅读 · 0 评论 -
Hive LEAD() LAG() 函数与partition by order by的结合
LAG() 表示当前行向上取, LEAD() 表示当前行向下取以LAG为例:LAG (scalar_expression [,offset] [,default]) OVER ( [ partition_by_clause ] order_by_clause ) 使用partition by和order by让数据先排好序,排好序以后使用LAG函数来对上序行取数。LAG第一个参数表示取上行的字段名,第二个字段表示取向上第几行,第三个参数表示值为空时的默认值(若同一个parition原创 2020-09-27 17:09:50 · 907 阅读 · 0 评论 -
Hive将时间格式转为时间戳函数
SELECT unix_timestamp(substr(time,1,19),‘yyyy-MM-dd HH:mm:ss’)*1000+substr(time,21,3),write_time from tableA原始时间格式:2020-09-15 10:35:33:841转换后时间戳:1600137333841原创 2020-09-18 14:50:55 · 1250 阅读 · 0 评论 -
HIVE SQL按照字符位置截取
需求:判断某个字符或字符串在字段中的位置,再根据位置开始截取instr函数:instr(string str,string substr) 返回字符串substr在str中首次出现的位置,若不出现则为0substr函数:substr(string A,int start) 字符串A从start位置到结尾的字符串substr(string A,int start,int len) 字符串A从start位置开始,长度为len的字符串SELECT CASE WHEN in原创 2020-08-13 14:56:01 · 5915 阅读 · 0 评论 -
使用HIVE SQL将多行数据按照指定字段为key排序
使用到的函数lpad(string str, int len, pad):返回值:string,说明:将str进行用pad进行左补到len位如:hive> select lpad(‘abc’,10,‘td’) from lxw1234;tdtdtdtabcover(partition by class order by sroce):按照sroce排序进行累计,order by是个默认的开窗函数,按照class分区。row_number 与over结合使用cast:类型转换co原创 2020-06-10 19:30:04 · 2188 阅读 · 0 评论