Hive 实用技巧(一)
count
count(if(status=1,true,null))中count函数返回一个布尔值类型的数值,如果status=1,返回true,会计数;如果status不等于1返回null,不会计数。
count(DISTINCT if(agent_begin_time < start_time
AND unix_timestamp(t4.start_time) - unix_timestamp(t4.agent_begin_time) <= 259200
AND order_no IS NOT NULL,session_id,NULL)) AS xxx
返回的是符合要求的session id,接distinct获得绝对数量,结果可能为x+1,因为null值。
instr and substr
字符串截取函数substr(strings|express,m,[n])
strings|express 被截取的字符串或字符串表达式
m 从第m个字符开始截取
n 截取后字符串长度为n
可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符。
其语法为:
instr(sourceString,destString,start,appearPosition). instr(‘源字符串’ , ‘目标字符串’ ,‘开始位置’,‘第几次出现’)
如果start的值为负数,那么代表从右往左进行查找,但是位置数据仍然从左向右计算。
返回值为:查找到的字符串的位置。
SQL> select instr('my 2 firefly','f',-1) result from dual;
RESULT
----------
10
if
if(expr1,exp

本文介绍了Hive的一些实用技巧,包括count函数在条件统计中的应用,instr和substr的字符串操作,if函数的条件判断,时间格式转换的cast函数使用,以及行转列的case when方法。还讨论了在Hive中找到事件发生后3天内的关联事件和数据库事务级别的理解。
最低0.47元/天 解锁文章
424

被折叠的 条评论
为什么被折叠?



