如何根据HiveQL和SQL中特定列的子字符串进行选择?

时间:2015-10-13 21:55:56

标签: sql hive substring hiveql

我在hive中有一张表

S.no  Age  minutes  code  
 1     10   20     75081     
 2     11   114    75080      
 3     21   104    75180     
 4     31   124    75108    
 5     10   20     75083     
 6     11   114    75180    
 7     21   104    75180    

我想编写一个hivesql / sql查询,该查询根据区域,即代码的前4位数,给出了总分钟的排序列表。 我该怎么办呢?我知道SUBSTRING()给了我所需的剪切,但我无法从那里去。

Select code, minutes as total  
from TableT   
where S.no > 1
group by code 
order by total

编辑: 基于邮政编码前4位的排名结果看起来应该是这样的

总代码

322(即104 + 114 + 104)7518
  154(即20 + 114 + 20)7508
124 7510

1 个答案:

答案 0 :(得分:8)

嗯。我以为你想要这个:

select substr(zip, 1, 4) as region, sum(minutes) as minutes,
       rank() over (order by sum(minutes) desc) as therank
from tableT
where s.no > 1
group by substr(zip, 1, 4)
order by minutes desc;