Frage

I'm looking at a SQL query that uses Oracle's rownum pseudocolumn to select the row number as a fraction of the total rows:

ROWNUM/(MAX(ROWNUM) OVER())

I'm trying to accomplish the same thing with a query written via jOOQ. Is that possible?

War es hilfreich?

Lösung 2

While Dmitry's CUME_DIST() solution is probably better suited for the actual query, here's the ROWNUM solution in jOOQ, for the record:

// Qualified
DSL.rownum().div(DSL.max(DSL.rownum()).over());

// With static imports of DSL.*
rownum().div(max(rownum()).over());

See also: DSL.rownum()

Andere Tipps

I'm not sure I got your question right - is it that you need ?

SQL> select row_number() over(order by ename)/(count(*) over()) fraction from emp;

  FRACTION                                                                      
----------                                                                      
    .08333                                                                      
    .16667                                                                      
    .25000                                                                      
    .33333                                                                      
    .41667                                                                      
    .50000                                                                      
    .58333                                                                      
    .66667                                                                      
    .75000                                                                      
    .83333                                                                      
    .91667                                                                      
   1.00000   

Also CUME_DIST analytic function can be useful:

SQL> SELECT ename, CUME_DIST()
  2     OVER (ORDER BY ename) fraction
  3     FROM emp
  4  /

ENAME        FRACTION                                                           
---------- ----------                                                           
ALLEN          .08333                                                           
BLAKE          .16667                                                           
CLARK          .25000                                                           
FORD           .33333                                                           
JAMES          .41667                                                           
JONES          .50000                                                           
KING           .58333                                                           
MARTIN         .66667                                                           
MILLER         .75000                                                           
SMITH          .83333                                                           
TURNER         .91667                                                          
WARD          1.00000      
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top