我有一个类似的表
表名称:任务
+---------+-------------+-------------
| Name | taskName | issueDate |
+---------+-------------+-------------
| A | AA | 4/11/2022 |
| A | AB | 4/11/2022 |
| A | AC | 4/11/2022 |
| B | BA | 3/11/2022 |
| B | BB | 3/11/2022 |
| B | BC | 3/11/2022 |
| C | CA | 2/11/2022 |
| C | CB | 2/11/2022 |
+---------+-------------+-------------
结果表将类似于此
+---------+-------------+------------
| Name | taskName | issueTime |
+---------+-------------+------------
| A | AA | 4/11/2022 |
| B | BA | 3/11/2022 |
| C | CA | 2/11/2022 |
| A | AB | 4/11/2022 |
| B | BB | 3/11/2022 |
| C | CB | 2/11/2022 |
| A | AC | 4/11/2022 |
| C | CB | 2/11/2022 |
+---------+-------------+-----------|
按照issueTime
的降序,首先显示唯一名称。
首先按照发布日期和任务名称的升序为每个"name"组提供一个行号,然后根据行号对结果集进行排序。
查询
select t.name, t.taskname, t.issuedate from(
select row_number() over(
partition by name
order by issuedate, taskname
) as rn, *
from tasks
) as t
order by t.rn, t.name;
此查询对于上述场景非常有用:
select name, taskname, issuedate from(
select name, taskname, issuedate, row_number() over(partition by name order by issuedate desc) as row_number
from task)
order by rownum;