第一个操作的行号
Row number for first action
我有一个 table,其中包含用户 ID 和操作日志。我想得到以下结果:
+---------+------------+---------+
| user_id | action_id | row_num |
+---------+------------+---------+
| id1 | action 1 | 1 |
| id1 | action 1 | 2 |
| id1 | action 2 | 1 |
| id1 | action 3 | 1 |
| id2 | action 1 | 1 |
| id2 | action 2 | 1 |
| id2 | action 3 | 1 |
| id2 | action 3 | 2 |
| id2 | action 3 | 3 |
+---------+------------+---------+
我很确定我需要使用 ROW_NUMBER() 函数,并尝试通过执行以下代码来实现:
select user_id,
action_id,
row_number() over (partition by action_id order by user_id desc) as rn
from table
但是我好像漏掉了什么。你能帮帮我吗?
我正在使用 Impala SQL 语法。
提前谢谢你。
你很接近。使用
row_number() over (partition by user_id,action_id order by action_id) as rn
首先在order by
子句中使用action_id
代替:
select user_id, action_id,
row_number() over (partition by user_id, action_id order by action_id) as rn
from table t;
其次,您没有在 partition 子句
中指定 action_id
我有一个 table,其中包含用户 ID 和操作日志。我想得到以下结果:
+---------+------------+---------+
| user_id | action_id | row_num |
+---------+------------+---------+
| id1 | action 1 | 1 |
| id1 | action 1 | 2 |
| id1 | action 2 | 1 |
| id1 | action 3 | 1 |
| id2 | action 1 | 1 |
| id2 | action 2 | 1 |
| id2 | action 3 | 1 |
| id2 | action 3 | 2 |
| id2 | action 3 | 3 |
+---------+------------+---------+
我很确定我需要使用 ROW_NUMBER() 函数,并尝试通过执行以下代码来实现:
select user_id,
action_id,
row_number() over (partition by action_id order by user_id desc) as rn
from table
但是我好像漏掉了什么。你能帮帮我吗? 我正在使用 Impala SQL 语法。
提前谢谢你。
你很接近。使用
row_number() over (partition by user_id,action_id order by action_id) as rn
首先在order by
子句中使用action_id
代替:
select user_id, action_id,
row_number() over (partition by user_id, action_id order by action_id) as rn
from table t;
其次,您没有在 partition 子句
中指定action_id