hive sql 小整理【持续更新】

1.查看某字段是否包含某字符串:

假设有一字段,"a",是string类型的,想看a字段中是否包含某一字符串,

用instr函数,若不包含,则返回0,若包含则返回该字符串所在位置,一定不为0;

举个例子,

#判断o字段是否包含“Android”,‘iOS’等,如果包含则对新的字段o1,o2,o3,o4,o5设置数值0或者1
select
case when ( instr('Android',o )>0 ) then 1 else 0 end as o1,
case when ( instr('iOS',o)>0      ) then 1 else 0 end as o2,
case when ( instr('Windows',o)>0  ) then 1 else 0 end as o3,
case when ( instr('Linux',o)>0    ) then 1 else 0 end as o4,
case when ( instr('Mac OS X',o)>0 ) then 1 else 0 end as o5,
*
from table

2.查看某一列出现次数最多前十名的数值或者字符串,

select device_model,count(*) as count from table group by device_model order by count desc limit 10

猜你喜欢

转载自blog.csdn.net/transformed/article/details/88573932
今日推荐