在配置单元 table 中为唯一 ID 添加一个新列
add a new column for unique ID in hive table
我在 hive 中有一个 table 有两列:session_id 和 duration_time 像这样:
|| session_id || duration||
1 14
1 10
1 20
1 10
1 12
1 16
1 8
2 9
2 6
2 30
2 22
我想在下列情况下添加具有唯一 ID 的新列:
session_id 正在改变 或 duration_time > 15
我希望输出是这样的:
session_id duration unique_id
1 14 1
1 10 1
1 20 2
1 10 2
1 12 2
1 16 3
1 8 3
2 9 4
2 6 4
2 30 5
2 22 6
关于如何在 hive QL 中做到这一点有什么想法吗?
谢谢!
SQL 表表示 无序 集。您需要一个指定值顺序的列,因为您似乎很关心顺序。例如,这可以是 id 列或 created-at 列。
您可以使用累计和来做到这一点:
select t.*,
sum(case when duration > 15 or seqnum = 1 then 1 else 0 end) over
(order by ??) as unique_id
from (select t.*,
row_number() over (partition by session_id order by ??) as seqnum
from t
) t;
我在 hive 中有一个 table 有两列:session_id 和 duration_time 像这样:
|| session_id || duration||
1 14
1 10
1 20
1 10
1 12
1 16
1 8
2 9
2 6
2 30
2 22
我想在下列情况下添加具有唯一 ID 的新列:
session_id 正在改变 或 duration_time > 15
我希望输出是这样的:
session_id duration unique_id
1 14 1
1 10 1
1 20 2
1 10 2
1 12 2
1 16 3
1 8 3
2 9 4
2 6 4
2 30 5
2 22 6
关于如何在 hive QL 中做到这一点有什么想法吗?
谢谢!
SQL 表表示 无序 集。您需要一个指定值顺序的列,因为您似乎很关心顺序。例如,这可以是 id 列或 created-at 列。
您可以使用累计和来做到这一点:
select t.*,
sum(case when duration > 15 or seqnum = 1 then 1 else 0 end) over
(order by ??) as unique_id
from (select t.*,
row_number() over (partition by session_id order by ??) as seqnum
from t
) t;