gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:40:53 25 4
gpt4 key购买 nike

这是表:

+------+------+
| 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 |
+-------+--------+-----+

有谁知道怎么做?

最佳答案

这不是最有效的方法,但确实有效。

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;

关于sql - Hive 查找组的开始和结束或更改点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36370624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com