SQL查询将记录分组到不同的字段下

时间:2011-04-14 06:48:19

标签: mysql sql

我正在使用MySQL,我有以下架构:

id school          round score win loss tie
2  My School Name  1     10    1   0    0
3  My School Name  2     20    0   1    0
4  My School Name  3     30    1   0    0
5  My School Name  4     40    1   0    0
6  My School Name  5     50    1   0    0
7  My School Name  6     60    0   0    1

我需要以下输出,按学校名称分组

School         Round1 Round2 Round3 Round4 Round5 Round6 wins losses ties
My School Name 10     20     30     40     50     60     4    1      1

到目前为止,我觉得我可以使用GROUP BY SchoolSUM(win) as wins来获取大部分功能。但是,困难的部分是获得那些Round_字段。

有谁知道怎么做?在此先感谢,任何帮助将不胜感激!

编辑:澄清一下,我知道我完全有10轮。

3 个答案:

答案 0 :(得分:1)

我们可以使用GROUP BY学校的SELECT语句为每所学校创建一条记录。正如您所指出的,使用SUM聚合函数可以很容易地计算关系,胜利和损失列。为了定位一个特定的回合,我们可以使用一些聪明的数学(以避免像CodeByMoonlight建议的那样详细的条件语句):

如果我们想要以R为目标,我们注意到“round-R”仅在round == R时为0,否则不为0.当我们取“round-R”的NOT时,0被反转到1时,其他一切都设置为0.现在,如果我们乘以!(round-R)乘以该轮的得分,当轮不是R(0 *得分= 0)时它将给出0并且它当轮次为R(1 *得分=得分)时,我们将给予“得分”。接下来,当我们在列上取这个值的SUM时,我们在round = R和0时添加得分,否则,有效地给我们圆R分数。

将所有这些放在一起给出了:

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`

其中RoundScores是相关表格。

编辑:

如果我们不想手动添加10,我们可以使用prepared statements

# 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;

答案 1 :(得分:0)

尝试 UNION (未经测试)

   SELECT SUM(win) AS wins, SUM(loss) AS losses, SUM(tie) AS ties
   FROM table 
   GROUP BY (school)

   UNION

   SELECT score AS round1 FROM table WHERE round=1

   UNION

   SELECT score AS round2 FROM table WHERE round=2
.... AND so on..

答案 2 :(得分:0)

SELECT School, Sum(Case
    When Round = 1 Then Score
    Else 0
End) AS Round1, Sum(Case
    When Round = 2 Then Score
    Else 0
End) AS Round2, Sum(Case
    When Round = 3 Then Score
    Else 0
End) AS Round3, Sum(Case
    When Round = 4 Then Score
    Else 0
End) AS Round4, Sum(Case
    When Round = 5 Then Score
    Else 0
End) AS Round5, Sum(Case
    When Round = 6 Then Score
    Else 0
End) AS Round6, Sum(Case
    When Round = 7 Then Score
    Else 0
End) AS Round7, Sum(Case
    When Round = 8 Then Score
    Else 0
End) AS Round8, Sum(Case
    When Round = 9 Then Score
    Else 0
End) AS Round9, Sum(Case
    When Round = 10 Then Score
    Else 0
End) AS Round10, Sum(Wins) AS Wins, Sum(Losses) AS Losses, Sum(Ties) AS Ties
FROM MyTable
GROUP BY School

应该工作:)