我有这张桌子。
+------------------------------------------------------------+
| ks | time | val1 | val2 |
+-------------+---------------+---------------+--------------+
| A | 1 | 1 | 1 |
| B | 1 | 3 | 5 |
| A | 2 | 6 | 7 |
| B | 2 | 10 | 12 |
| A | 4 | 6 | 7 |
| B | 4 | 20 | 26 |
+------------------------------------------------------------+
我想要得到的是每一行,
ks | time | val1 | val1 of next ts of same ks |
需要明确的是,上述示例的结果应该是,
+------------------------------------------------------------+
| ks | time | val1 | next.val1 |
+-------------+---------------+---------------+--------------+
| A | 1 | 1 | 6 |
| B | 1 | 3 | 10 |
| A | 2 | 6 | 6 |
| B | 2 | 10 | 20 |
| A | 4 | 6 | null |
| B | 4 | 20 | null |
+------------------------------------------------------------+
(对于 value2 我也需要同样的下一个)
我尝试了很多来为此提出一个蜂巢查询,但仍然没有运气。如here (Quassnoi's answer)所述,我能够在sql中为此编写查询,但无法在hive中创建等效项,因为hive不支持select中的子查询。
有人可以帮我实现这一目标吗?
提前致谢。
编辑:
我试过的查询是,
SELECT ks, time, val1, next[0] as next.val1 from
(SELECT ks, time, val1
COALESCE(
(
SELECT Val1, time
FROM myTable mi
WHERE mi.val1 > m.val1 AND mi.ks = m.ks
ORDER BY time
LIMIT 1
), CAST(0 AS BIGINT)) AS next
FROM myTable m
ORDER BY time) t2;