3

我正在尝试在 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:在这个例子中,格式日期是(dd/mm/yy)

正如您在示例中看到的,首先按日期排序,但是当捕获第一行时,列出具有相同请求的所有行,当完成具有相同请求的行时,继续按日期排序...

谁能帮我解决这个问题?我尝试了几种方法,但没有预期的结果。

4

1 回答 1

8

您可以使用分析

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
于 2012-12-05T13:05:54.207 回答