What does PARTITION BY 1 mean?

Eve*_*one 7 oracle analytics count database-partitioning

For a pair of cursors where the total number of rows in the resultset is required immediately after the first FETCH, ( after some trial-and-error ) I came up with the query below

SELECT
 col_a,
 col_b,
 col_c,
 COUNT(*) OVER( PARTITION BY 1 ) AS rows_in_result
FROM 
 myTable JOIN theirTable ON
 myTable.col_a = theirTable.col_z
GROUP BY
 col_a, col_b, col_c
ORDER BY 
 col_b
Run Code Online (Sandbox Code Playgroud)

Now when the output of the query is X rows, rows_in_result reflects this accurately.

  • What does PARTITION BY 1 mean?
    • I think it probably tells the database to partition the results into pieces of 1-row each

Ton*_*ews 12

这是PARTITION BY的一个不寻常的用法.它的作用是将所有内容放在同一个分区中,这样如果查询一共返回123行,那么每行的rows_in_result值将为123(如别名所示).

因此它相当于更简洁:

COUNT(*) OVER ()
Run Code Online (Sandbox Code Playgroud)


Luk*_*der 5

数据库可以很自由地向OVER()子句添加限制。有时,两者之一PARTITION BY [...]和/或是ORDER BY [...]强制性子句,具体取决于聚合函数。PARTITION BY 1可能只是一个用于语法完整性的虚拟子句。以下两个通常是等价的:

[aggregate function] OVER ()
[aggregate function] OVER (PARTITION BY 1)
Run Code Online (Sandbox Code Playgroud)

但请注意,Sybase SQL Anywhere 和CUBRID将此解释1为列索引引用,类似于ORDER BY [...]子句中可能出现的情况。这可能看起来有点令人惊讶,因为它对查询的投影施加了评估顺序。在您的情况下,这意味着以下内容是等效的

COUNT(*) OVER (PARTITION BY 1)
COUNT(*) OVER (PARTITION BY col_a)
Run Code Online (Sandbox Code Playgroud)

这种与其他数据库解释的奇怪偏差允许引用更复杂的分组表达式。