简体   繁体   English

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

[英]How to do select based on a substring of a particular column in HiveQL and SQL?

I have a table in 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    

I want to write an hivesql/sql query that gives ranked list of total minutes spoken based on the region ie first 4 digits of code. 我想编写一个hivesql / sql查询,该查询根据区域(即代码的前4位数)给出所说的总分钟的排序列表。 How should i go about it? 我该怎么办呢? I know that SUBSTRING() gives me the required cut, but I have not been able to go from there. 我知道SUBSTRING()给了我所需的剪切,但是我无法从那里开始。

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

Edit: The ranked results based on first 4 digits of zipcode should look something like this 编辑:基于邮政编码前4位数的排名结果应如下所示

total code 总代码

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

Hmmm. 嗯。 I'm thinking you want this: 我想你想要这个:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM