Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't ORDER BY the ROW_NUMBER directly: I don't know why you didn't get an error on this case, but normally you would. Hence the use of derived tables or CTEs</p> <pre><code>SELECT name, number FROM ( SELECT name, ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) as number, AVG(mark) OVER (PARTITION BY name) AS nameavg FROM table ) foo ORDER BY nameavg, number </code></pre> <p>However, PARTITION BY name ORDER BY name is meaningless. Each partition has random order because the sort <em>is</em> the partition</p> <p>I suspect you want something like this where ROW_NUMBER is based on AVG</p> <pre><code>SELECT name, number FROM ( SELECT name, ROW_NUMBER() OVER (PARTITION BY name ORDER BY nameavg) AS number FROM ( SELECT name, AVG(mark) OVER (PARTITION BY name) AS nameavg FROM table ) foo ) bar ORDER BY number </code></pre> <p>Or more traditionally (but name is collapsed for the average)</p> <pre><code>SELECT name, number FROM ( SELECT name, ROW_NUMBER() OVER (PARTITION BY name ORDER BY nameavg) AS number FROM ( SELECT name, AVG(mark) AS nameavg FROM table GROUP BY name ) foo ) bar ORDER BY number </code></pre> <p>You can <em>maybe</em> collapse the derived foo and bar into one with </p> <pre><code>ROW_NUMBER() OVER (PARTITION BY name ORDER BY AVG(mark)) </code></pre> <p>But <em>none</em> of this makes sense: I understand that your question is abstract about <em>how</em> it works bit it is unclear question. It would make more sense if you described what you want in plain English and with sample input and output</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload