>我有一个包含用户 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语法。
提前谢谢你。
首先在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;
其次,您尚未在分区子句中指定action_id
你很接近。用
row_number() over (partition by user_id,action_id order by action_id) as rn