Hive查找组的开始和结束或更改点

时间:2016-04-02 07:44:05

标签: sql group-by hive hiveql boundary

这是表格:

+------+------+
| Name | Time |   
+------+------+
| A    |    1 |   
| A    |    2 |   
| A    |    3 |   
| A    |    4 |   
| B    |    5 |   
| B    |    6 |   
| A    |    7 |   
| B    |    8 |   
| B    |    9 |   
| B    |   10 |   
+------+------+

我想写一个查询来获取:

+-------+--------+-----+
| Name  | Start  | End |
+-------+--------+-----+
| A     |     1  |   4 |
| B     |     5  |   6 |
| A     |     7  |   7 |
| B     |     8  |  10 |
+-------+--------+-----+

有谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

这不是最有效的方式,但它可行。

SELECT name, min(time) AS start,max(time) As end 
FROM (
   SELECT name,time, time- DENSE_RANK()  OVER (partition by name ORDER BY 
   time) AS diff
   FROM foo
) t 
GROUP BY name,diff;

我建议尝试以下查询并构建一个GenericUDF来识别差距,这样更容易:)

SELECT name, sort_array(collect_list(time)) FROM foo GROUP BY name;