Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We can use a SELECT statement with a GROUP BY school to create a record for each school. The ties, wins, and losses columns are readily calculated with the SUM aggregate function, as you noted. To target a specific round, we can use some clever math (to avoid verbose conditional statements like the one CodeByMoonlight suggested):</p> <p>If we want to target round R, we note that "round-R" is 0 only when round == R, otherwise it isn't 0. When we take the NOT of "round-R", 0 gets inverted to 1, while everything else gets set to 0. Now, if we multiply !(round-R) by the score of that round, it will give us 0 when the round is not R (as 0*score = 0) and it will give us "score" when the round is R (as 1*score = score). Next, when we take the SUM of this value over the columns, we add score when round=R and 0 otherwise, effectively giving us just the round R score. </p> <p>Putting that all together gives:</p> <pre><code>SELECT school AS `School`, SUM(!(round-1)*score) AS `Round1`, SUM(!(round-2)*score) AS `Round2`, SUM(!(round-3)*score) AS `Round3`, SUM(!(round-4)*score) AS `Round4`, SUM(!(round-5)*score) AS `Round5`, SUM(!(round-6)*score) AS `Round6`, SUM(!(round-7)*score) AS `Round7`, SUM(!(round-8)*score) AS `Round8`, SUM(!(round-9)*score) AS `Round9`, SUM(!(round-10)*score) AS `Round10`, SUM(win) AS `wins`, SUM(loss) AS `losses`, SUM(tie) AS `ties` FROM `RoundScores` GROUP BY `school` </code></pre> <p>where RoundScores is the table in question.</p> <p>EDIT:</p> <p>If we do not want to manually add 10, we can use <a href="http://dev.mysql.com/tech-resources/articles/4.1/prepared-statements.html" rel="nofollow">prepared statements</a> :</p> <pre><code># Store all the conditionals in a string: # I was not able to to have round loop from 1 to 10, so I iterated over # all distinct values of 'round' present in the table. SET @s = ""; SELECT `round`, (@s := CONCAT( @s , "SUM(!(round-",round, ")*score) AS `Round",round, "`," )) FROM `RoundScores` GROUP BY `round`; # Combine the conditionals from before with the rest of the statement needed. SET @qry = CONCAT("SELECT school AS `School`,",@s,"SUM(win) AS `wins`,SUM(loss) AS `losses` FROM `RoundScores` GROUP BY `school`"); # Prepare and execute the statement. PREPARE stmt1 FROM @qry; EXECUTE stmt1; </code></pre>
    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.
    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