Note that there are some explanatory texts on larger screens.

plurals
  1. POT-SQL Combine Multiple Rows Into Single Row
    text
    copied!<p>I have this T-SQL (simplified):</p> <pre><code>select 3.00 as score1, null as score2, null as score3, null as score4 union all select null as score1, 4.5 as score2, 1.5 as score3, null as score4 </code></pre> <p>Which produces this:</p> <pre><code>score1 score2 score3 score4 ------------------------------ 3.00 NULL NULL NULL NULL 4.5 1.5 NULL </code></pre> <p>But i want to join it into one row, like this:</p> <pre><code>score1 score2 score3 score4 ------------------------------ 3.00 4.5 1.5 NULL </code></pre> <p>Sorry - im drawing blank (it's late in the day). </p> <p>Do i need a temporary table?</p> <p>The reason i'm doing this is that i have the following association:</p> <p><strong>Reviews 1..* Scores</strong></p> <p>So a regular join produces 1 row for each Score - but i want to insert a record (into another table), that has a column for each record - if you know what i mean:</p> <pre><code>INSERT INTO OtherTable (ReviewId, Score1, Score2, Score3, Score4) ???? </code></pre> <p>Hope that makes sense.</p> <p><strong>EDIT</strong></p> <p>Based on @OMG Ponies answer (which he just removed), i came up with this:</p> <pre><code>SELECT CASE MAX(x.score1) WHEN 0 THEN NULL ELSE MAX(x.score1) END AS score4 CASE MAX(x.score2) WHEN 0 THEN NULL ELSE MAX(x.score2) END AS score4 CASE MAX(x.score3) WHEN 0 THEN NULL ELSE MAX(x.score3) END AS score4 CASE MAX(x.score4) WHEN 0 THEN NULL ELSE MAX(x.score4) END AS score4 FROM (select 3.00 as score1, 0 as score2, 0 as score3, 0 as score4 union all select 0 as score1, 4.5 as score2, 1.5 as score3, 0 as score4) x </code></pre> <p>But it's pretty ugly. Any other ideas? </p>
 

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