Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming the <code>tsqlt</code> tag was meant to be <code>tsql</code>, and further that this implies that this is for SQL server:</p> <pre><code>;with Numbered as ( select Name,Value, ROW_NUMBER() OVER (ORDER BY Value DESC) as rnDesc, ROW_NUMBER() OVER (ORDER BY Value ASC) as rnAsc from @t ), MixedNumbered as ( select Name,Value, CASE WHEN rnDesc &lt; rnAsc THEN rnDesc ELSE rnAsc END as rnFin, rnAsc, rnDesc from Numbered ) select Name,Value from MixedNumbered order by rnFin,rnDesc </code></pre> <p>This works by finding the row numbers whilst considering the list sorted both highest-to-lowest and lowest-to-highest (in <code>Numbered</code>, <code>rnDesc</code> and <code>rnAsc</code>). We then take the lowest row number that was achieved when considering either of these orderings (<code>MixedNumbered</code>, <code>rnFin</code>).</p> <p>This should then, produce two rows with an <code>rnFin</code> equal to 1, two rows with it equal to 2, and so on; pairing the <i>n</i>th highest and <i>n</i>th lowest rows until we reach the middle of the set.</p> <p>We then use this to sort the final result set - but use the position obtained by considering the values sorted highest-to-lowest (<code>rnDesc</code>) as the tie breaker between each pair of rows with the same <code>rnFin</code> value. This means, for each pair, that the higher valued row will appear first.</p> <p>To reverse the result (lowest first, then highest, the second lowest, second highest, etc), we need only change the final <code>ORDER BY</code> clause to <code>rnFin,rnAsc</code>.</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. 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