Redshift 获取具有最高优先级和时间戳的行

Redshift get rows with highest priority and timestamp

我有一个table在redshift中,记录如下


+====+======+=========+============+
| Id | Book | Action  | Timestamp  |
+====+======+=========+============+
| 1  | ABC  | ADDED   | 1111111111 |
+----+------+---------+------------+
| 1  | ABC  | REMOVED | 2222222222 |
+----+------+---------+------------+
| 1  | ABC  | CHECKED | 3333333333 |
+----+------+---------+------------+
| 2  | XYZ  | ADDED   | 4444444444 |
+----+------+---------+------------+
| 2  | XYZ  | CHECKED | 5555555555 |
+----+------+---------+------------+
| 5  | DEF  | CHECKED | 6666666666 |
+----+------+---------+------------+
| 5  | DEF  | CHECKED | 7777777777 |
+====+======+=========+============+

我想要的是为每本书获取一条记录。在重复的情况下,按具有最低等级的 CHECKED 的操作确定优先级,其余所有操作将具有相同的优先级,然后选择最近的记录。

预期输出


+====+======+=========+============+
| Id | Book | Action  | Timestamp  |
+====+======+=========+============+
| 1  | ABC  | REMOVED | 2222222222 |
+----+------+---------+------------+
| 2  | XYZ  | ADDED   | 4444444444 |
+----+------+---------+------------+
| 5  | DEF  | CHECKED | 7777777777 |
+====+======+=========+============+

一个简单的方法使用row_number():

select t.*
from (select t.*,
             row_number() over (partition by id
                                order by (case action when 'Checked' then 1 else 2 end) desc,
                                         timestamp desc
                               ) as seqnum
      from t
     ) t
where seqnum = 1;