DENSE_RANK ORDER BY NULL 总是 return 1

DENSE_RANK ORDER BY NULL always return 1

我在 PROD 程序代码中发现:

select *
from (select "EMPNO",
    "SAL",
    "COMM",
    "DEPTNO",
    DENSE_RANK() OVER (PARTITION BY deptno ORDER BY null) AS drank
    from SCOTT."EMP"
    where deptno in (10,30))
where drank = 1
order by deptno

结果:

EMPNO   SAL      COMM  DEPTNO   DRANK
7934    1300     -     10       1
7839    5000     -     10       1
7782    2450     -     10       1
7844    1500     0     30       1
7900    950      -     30       1
7654    1250     1400  30       1
7499    1600     300   30       1
7698    2850     -     30       1
7521    1250     500   30       1

结果 drank 始终等于 1。这也适用于:

DENSE_RANK() OVER (ORDER BY null) AS drank

DENSE_RANK() OVER (PARTITION BY comm ORDER BY null) AS drank

DENSE_RANK() OVER (PARTITION BY 1 ORDER BY null) AS drank

DENSE_RANK() OVER (PARTITION BY null ORDER BY null) AS drank

ORDER BY null子句时drank不等于1的情况吗?

编辑:我知道 dense_rank 从 1 开始。问题是关于大于 1 的值。

The documentation 说:

Use the order_by_clause to specify how data is ordered within a partition. For all analytic functions you can order the values in a partition on multiple keys, each defined by a value_expr and each qualified by an ordering sequence.

Within each function, you can specify multiple ordering expressions. Doing so is especially useful when using functions that rank values, because the second expression can resolve ties between identical values for the first expression.

Whenever the order_by_clause results in identical values for multiple rows, the function behaves as follows:

  • CUME_DIST, DENSE_RANK, NTILE, PERCENT_RANK, and RANK return the same result for each of the rows.
  • ...

当您 order by null 时,order_by_clausemultiple 产生相同的值所有 行(在分区中),所以它们都得到相同的结果。

documentation for dense_rank 还说:

The ranks are consecutive integers beginning with 1.

所以他们得到相同的结果,必须是 1。