提问人:GoGoGo 提问时间:4/2/2016 最后编辑:ekadGoGoGo 更新时间:7/3/2017 访问量:350
Hive 查找组或更改点的起点和终点
Hive Find Start and End of Group or Changing point
问:
下表如下:
+------+------+
| 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 |
+-------+--------+-----+
有谁知道怎么做?
答:
0赞
hlagos
1/13/2017
#1
这不是最有效的方法,但它是有效的。
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;
评论
HIVE
SQL Server