Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder by Column, Then Row and taking into account non unique column values
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4221595/ordered-value-by-column-row">Ordered Value by Column / Row</a> </p> </blockquote> <p>Its a little difficult to explain. It might be easier to skip to the examples.</p> <p>A table has an id and four columns that each allow null. </p> <pre><code>ID, Col1, Col2, Col3, Col4 </code></pre> <p>There are x number of rows. ( Usually less than 4 ) And only up to 4 distinct values will be used across the columns.</p> <p>I am looking to return up to 4 rows, where each row in the resulting set is basically a column value where the value is selected left from right starting at the top preserving Col number. If another row has a value that is not column unique it is shifted to the next available column.</p> <p>Examples:</p> <p>If I have:</p> <pre><code>ID, Col1, Col2, Col3, Col4 0, A , B , , D 1, , , C , </code></pre> <p>I would like to return</p> <pre><code>A B C D </code></pre> <p>and</p> <pre><code>ID, Col1, Col2, Col3, Col4 0, A , B , C , 1, D , , , </code></pre> <p>Gives</p> <pre><code>A B C D </code></pre> <p>Thanks! The scenario when there are non unique columns and spaces between values can be thrown out.<br> This will not happen:</p> <pre><code>a,b,,d c,,, </code></pre> <p>This might help:</p> <pre><code>CREATE TABLE #original ( id int ,A INT, B INT, C INT, D INT ); INSERT INTO #original SELECT 0,1,2,null,4 union select 1,null,null,3,null --SELECT 0,1,2,3,null --union --select 1,4,null,null,null select * from #original order by id asc DROP TABLE #original </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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