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> sel