我正在尝试在pl / sql中创建一个查询以获得以下结果:
数据:
Id | Date | Request
1 | 10/09/10 | 111
2 | 09/09/10 | 100
3 | 13/09/10 | 111
4 | 01/08/10 | 222
5 | 08/07/10 | 100
6 | 06/07/10 | 111
7 | 04/07/10 | 100
8 | 01/06/10 | 222
获得以下结果:
Id | Date | Request
8 | 01/06/10 | 222
4 | 01/08/10 | 222
7 | 04/07/10 | 100
2 | 09/09/10 | 100
5 | 08/07/10 | 100
6 | 06/07/10 | 111
1 | 10/09/10 | 111
3 | 13/09/10 | 111
Ps:在这个例子中,格式日期是(年/月/日)
正如您在示例中所看到的,首先按日期排序,但是当捕获第一行时,列出具有相同请求的所有行,当完成具有相同请求的行时,按日期保持排序...
有人可以帮我解决这个问题吗?我尝试了几种方法,但没有预期的结果。
答案 0 :(得分:8)
您可以使用analytics:
SQL> WITH data AS (
2 SELECT 8 id, to_date('01/06/10') dat, 222 req FROM DUAL
3 UNION ALL SELECT 4, to_date('01/08/10'), 222 FROM DUAL
4 UNION ALL SELECT 7, to_date('04/07/10'), 100 FROM DUAL
5 UNION ALL SELECT 2, to_date('09/09/10'), 100 FROM DUAL
6 UNION ALL SELECT 5, to_date('08/07/10'), 100 FROM DUAL
7 UNION ALL SELECT 6, to_date('06/07/10'), 111 FROM DUAL
8 UNION ALL SELECT 1, to_date('10/09/10'), 111 FROM DUAL
9 UNION ALL SELECT 3, to_date('13/09/10'), 111 FROM DUAL
10 )
11 SELECT ID, dat, req
12 FROM DATA
13 ORDER BY MIN(dat) over (PARTITION BY req), req, dat;
ID DAT REQ
---------- ----------- ----------
8 01/06/2010 222
4 01/08/2010 222
7 04/07/2010 100
5 08/07/2010 100
2 09/09/2010 100
6 06/07/2010 111
1 10/09/2010 111
3 13/09/2010 111