Oracle Fetch下一行按唯一ID的限制

zbwhf8kr  于 2023-05-28  发布在  Oracle
关注(0)|答案(3)|浏览(108)

我有一个表,其中包含如下所示的数据,

+-------+----------------+----------------+
|  Id   |    TierUnitId  |  ObjectNumber  |
+-------+----------------+----------------+
|  10   |     3599       |     1          |
|  10   |     3599       |     2          |
|  20   |     3599       |     3          |
|  20   |     3599       |     4          |
|  20   |     3599       |     1          |
|  30   |     3599       |     2          |
|  30   |     3599       |     3          |
+-------+----------------+----------------+

我有一个选择查询

SELECT ID FROM TEST
FETCH NEXT :LIMIT ROWS ONLY

现在我想用limit的值来限制行数。当Limit的值为2时,我希望有两个不同的ID,即最多5行。然而,从查询中,我只得到两行ID为10的行。有人能帮助我限制使用不同id的行吗?我想要的是输出中的不同ID的总数是有限的。

8zzbczxx

8zzbczxx1#

使用DENSE_RANK解析函数根据唯一/不同的ID值对行进行编号,然后对其进行过滤:

SELECT id
FROM   (
  SELECT ID,
         DENSE_RANK() OVER (ORDER BY id) AS rnk
  FROM   test
)
WHERE  rnk <= 2;

其中,对于样本数据:

CREATE TABLE test (Id, HierUnitId, ObjectNumber ) AS
SELECT 10, 3599, 1 FROM DUAL UNION ALL
SELECT 10, 3599, 2 FROM DUAL UNION ALL
SELECT 20, 3599, 3 FROM DUAL UNION ALL
SELECT 20, 3599, 4 FROM DUAL UNION ALL
SELECT 20, 3599, 1 FROM DUAL UNION ALL
SELECT 30, 3599, 2 FROM DUAL UNION ALL
SELECT 30, 3599, 3 FROM DUAL;

输出:
| ID|
| - -----|
| 十个|
| 十个|
| 二十个|
| 二十个|
| 二十个|

rwqw0loc

rwqw0loc2#

正如你在评论中所说,你需要能够定义应该显示多少个不同的id。对于这种情况,我建议您先找到这些ID(请参阅distinct_ids部分),然后再获取所需的所有行

with distinct_ids as (
  select distinct id
    from test_data  
   order by id
   fetch first :limit rows only)  

select id
  from test_data td
  join distinct_ids di
    on td.id = di.id
vybvopom

vybvopom3#

如果你需要 * 一些 * 不同的ID,没有任何特定的顺序,那么你可以把fetch next ...放进子查询中,关键字是distinctID列上的索引将适合于避免两次全表扫描(我假设ID不能为空)

select /*+gather_plan_statistics*/
  *
from t
where id in (
  select distinct id
  from t
  where id is not null
  fetch next 2 rows only
)
ID | HIERUNITID | OBJECTNUMBER
-: | ---------: | -----------:
 1 |       3599 |            1
 1 |       3599 |            2
 2 |       3599 |            3
 2 |       3599 |            4
 2 |       3599 |            1
select *
from table(dbms_xplan.display_cursor(
  format => 'ALL -PROJECTION -ALIAS ALLSTATS LAST'
))
| PLAN_TABLE_OUTPUT                                                                                                                                              |
| :------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| SQL_ID  2sqqq53kpy5rj, child number 0                                                                                                                          |
| -------------------------------------                                                                                                                          |
|  select /*+gather_plan_statistics*/   * from t where id in (   select                                                                                          |
| distinct id   from t   where id is not null   fetch next 2 rows only )                                                                                         |
|                                                                                                                                                                |
| Plan hash value: 534568331                                                                                                                                     |
|                                                                                                                                                                |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| | Id  | Operation                    | Name     | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem | |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| |   0 | SELECT STATEMENT             |          |      1 |        |       |     5 (100)|          |      5 |00:00:00.01 |       3 |       |       |          | |
| |   1 |  MERGE JOIN SEMI             |          |      1 |      5 |   115 |     5  (40)| 00:00:01 |      5 |00:00:00.01 |       3 |       |       |          | |
| |   2 |   TABLE ACCESS BY INDEX ROWID| T        |      1 |      7 |    70 |     2   (0)| 00:00:01 |      6 |00:00:00.01 |       2 |       |       |          | |
| |   3 |    INDEX FULL SCAN           | T_IX     |      1 |      7 |       |     1   (0)| 00:00:01 |      6 |00:00:00.01 |       1 |       |       |          | |
| |*  4 |   SORT UNIQUE                |          |      6 |      2 |    26 |     3  (67)| 00:00:01 |      5 |00:00:00.01 |       1 |  2048 |  2048 | 2048  (0)| |
| |   5 |    VIEW                      | VW_NSO_1 |      1 |      2 |    26 |     2  (50)| 00:00:01 |      2 |00:00:00.01 |       1 |       |       |          | |
| |*  6 |     VIEW                     |          |      1 |      2 |    20 |     2  (50)| 00:00:01 |      2 |00:00:00.01 |       1 |       |       |          | |
| |*  7 |      WINDOW NOSORT STOPKEY   |          |      1 |      3 |    39 |     2  (50)| 00:00:01 |      2 |00:00:00.01 |       1 | 73728 | 73728 |          | |
| |   8 |       VIEW                   |          |      1 |      3 |    39 |     2  (50)| 00:00:01 |      2 |00:00:00.01 |       1 |       |       |          | |
| |   9 |        SORT UNIQUE NOSORT    |          |      1 |      3 |     9 |     2  (50)| 00:00:01 |      2 |00:00:00.01 |       1 |       |       |          | |
| |* 10 |         INDEX FULL SCAN      | T_IX     |      1 |      7 |    21 |     1   (0)| 00:00:01 |      6 |00:00:00.01 |       1 |       |       |          | |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|                                                                                                                                                                |
| Predicate Information (identified by operation id):                                                                                                            |
| ---------------------------------------------------                                                                                                            |
|                                                                                                                                                                |
|    4 - access("ID"="ID")                                                                                                                                       |
|        filter("ID"="ID")                                                                                                                                       |
|    6 - filter("from$_subquery$_004"."rowlimit_$$_rownumber"<=2)                                                                                                |
|    7 - filter(ROW_NUMBER() OVER ( ORDER BY NULL)<=2)                                                                                                           |
|   10 - filter("ID" IS NOT NULL)                                                                                                                                |
|                                                                                                                                                                |

相关问题