查询以查找特定时间间隔中的元素

时间:2015-07-01 18:16:39

标签: mysql group-by hive

我需要找到一个Hive查询,该查询返回自00:00:00以来每6分钟间隔的2个顶级名称,

数据看起来像 -

Name    Time
A   00:00:00
B   00:03:53
C   00:01:16
A   00:04:34
A   00:07:32
A   00:18:36
C   00:16:12
C   00:05:04
B   00:01:50
B   00:12:05
A   00:11:20
B   00:04:27
B   00:02:47
A   00:00:23
A   00:00:23
B   00:36:21
B   00:02:46

我想在Hive中编写查询,这对我来说很新,但即使使用mysql查询,我也可以在Hive中得到结果。

1 个答案:

答案 0 :(得分:1)

select 
* 
from
(
select NAME
, time_interval_6
, rank() over (partition by NAME, time_interval_6 order by ct desc) as ranking
from
  (select count(1) as ct
  ,  NAME
  ,  floor((floor(cast(substring(time,1,2) as int)*60 + cast(substring(time,4,2) as int)))/6) as time_interval_6
FROM MY_TABLE
  group by NAME,  floor((floor(cast(substring(time,1,2) as int)*60 + cast(substring(time,4,2) as int)))/6)
   ) a
)b
where ranking <= 2
;