2

我的桌子

+------+-------+---------+-------+--------+
| Name | Group1| Section | Marks | Points |
+------+-------+---------+-------+--------+
| S1   | G1    | class1  |    55 |        |
| S16  | G1    | class1  |    55 |        |
| S17  | G1    | class1  |    55 |        |
| S28  |       | class1  |    55 |        |
| S2   |       | class2  |    33 |        |
| S3   |       | class1  |    25 |        |
| S4   | G88   | class2  |    65 |        |
| S5   | G88   | class2  |    65 |        |
| S30  | G66   | class2  |    66 |        |
| S31  | G66   | class2  |    66 |        |
| S32  |       | class1  |    65 |        |
| S7   | G5    | class1  |    32 |        |
| S18  | G5    | class1  |    32 |        |
| S19  | G5    | class1  |    32 |        |
| S33  | G4    | class2  |    60 |        |
| S34  | G4    | class2  |    60 |        |
| S35  | G4    | class2  |    60 |        |
| S10  |       | class2  |    78 |        |
| S8   | G8    | class1  |    22 |        |
| S20  | G8    | class1  |    22 |        |
| S21  | G8    | class1  |    22 |        |
| S9   |       | class2  |    11 |        |
| S12  |       | class3  |    43 |        |
| S22  | G9    | class1  |    20 |        |
| S23  | G9    | class1  |    20 |        |
| S24  | G9    | class1  |    20 |        |
| S13  | G55   | class2  |    33 |        |
| S36  | G55   | class2  |    33 |        |
| S14  |       | class2  |    78 |        |
| S25  | G10   | class1  |    55 |        |
| S26  | G10   | class1  |    55 |        |
| S27  | G10   | class1  |    55 |        |
+------+-------+---------+-------+--------+

SQL FIDDLE:http ://www.sqlfiddle.com/#!2/5ce6c/1

我正在尝试为每个部分中得分最高的前 3 组给出具体的分数。我想给第一高组中的每个学生加 5 分,第二高组加 3 分,第三高组加 1 分。. 组可能出现重复标记。

我正在使用以下代码,此代码适用于个别学生,不知道如何给小组积分。

select t1.Name, t1.Section, t1.Marks from myTable t1 join 
(select Section, substring_index(group_concat (distinct Marks order by Marks desc),    
',', 3) as Marks3  from myTable where Section = 'class1' group by Section ) tsum  
on t1.Section = tsum.Section and find_in_set(t1.Marks, tsum.Marks3) > 0 
ORDER BY Section, Marks DESC, ID Desc

我的最终输出寻找一个部分。

 +---------------------------------------------+
 | | Name | Group1| Section | Marks | Points | |
 +---------------------------------------------+
 | | S1   | G1    | class1  |    55 |    5   | |
 | | S16  | G1    | class1  |    55 |    5   | |
 | | S17  | G1    | class1  |    55 |    5   | |
 | | S28  |       | class1  |    55 |        | |
 | | S2   |       | class2  |    33 |        | |
 | | S3   |       | class1  |    25 |        | |
 | | S4   | G88   | class2  |    65 |        | |
 | | S5   | G88   | class2  |    65 |        | |
 | | S30  | G66   | class2  |    66 |        | |
 | | S31  | G66   | class2  |    66 |        | |
 | | S32  |       | class1  |    65 |        | |
 | | S7   | G5    | class1  |    32 |    3   | |
 | | S18  | G5    | class1  |    32 |    3   | |
 | | S19  | G5    | class1  |    32 |    3   | |
 | | S33  | G4    | class2  |    60 |        | |
 | | S34  | G4    | class2  |    60 |        | |
 | | S35  | G4    | class2  |    60 |        | |
 | | S10  |       | class2  |    78 |        | |
 | | S8   | G8    | class1  |    22 |   1    | |
 | | S20  | G8    | class1  |    22 |   1    | |
 | | S21  | G8    | class1  |    22 |   1    | |
 | | S9   |       | class2  |    11 |        | |
 | | S12  |       | class3  |    43 |        | |
 | | S22  | G9    | class1  |    20 |        | |
 | | S23  | G9    | class1  |    20 |        | |
 | | S24  | G9    | class1  |    20 |        | |
 | | S13  | G55   | class2  |    33 |        | |
 | | S36  | G55   | class2  |    33 |        | |
 | | S14  |       | class2  |    78 |        | |
 | | S25  | G10   | class1  |    55 |   5    | |
 | | S26  | G10   | class1  |    55 |   5    | |
 | | S27  | G10   | class1  |    55 |   5    | |
 +---------------------------------------------+

请帮我。

4

3 回答 3

3

那很有挑战性。

为了解决它,我使用了几种方法:

  1. 将小组在前 3 名中的位置转换为积分的CASE语句。
  2. 使用变量编号的行
  3. INNER AND LEFT JOIN将结果合并在一起。

以下查询已在您的小提琴上进行了测试并且有效:

SELECT t1.`id`,  t1.`name`,  t1.`group1`,
       t1.`section`, t1.`MARKS`, `t_group_points`.`points`

FROM   `students` t1

#--- Join  groups' points to the students
LEFT JOIN (
    (
        #---- Join all groups and give points to top 3 avg's groups ----
        SELECT `t4`.`group1`, `t_points`.`points`
        FROM   (SELECT   `t3`.`group1`, AVG(`t3`.`marks`) AS `avg`
                FROM     `students` `t3`
                WHERE    (`t3`.`section` = 'class1') AND
                         (`t3`.`group1` IS NOT NULL)
                GROUP BY `t3`.`group1`) `t4`

        INNER JOIN (
              #---------- Select top 3 avarages ----------
              (SELECT `top`.`avg`,
                      #-- Convert row number to points ---
                      CASE @curRow := @curRow + 1  
                           WHEN '1' THEN 5
                           WHEN '2' THEN 3
                           WHEN '3' THEN 1
                           ELSE NULL END 'points'

              FROM (SELECT DISTINCT `t_avg`.`avg`
                    FROM   (SELECT   `t2`.`group1`, AVG(`t2`.`marks`) AS `avg`
                            FROM     `students` `t2`
                            WHERE    (`t2`.`section` = 'class1') AND
                                     (`t2`.`group1` IS NOT NULL)
                            GROUP BY `group1`) `t_avg`
                    ORDER BY `avg` DESC
                    LIMIT 0, 3) `top`, (SELECT @curRow:=0) r
              ) AS `t_points`)
         ON (`t_points`.`avg` = `t4`.`avg`)      
    ) AS `t_group_points`)
ON (`t_group_points`.`group1` = `t1`.`group1`)
于 2013-08-23T14:44:33.990 回答
1

我找到了一个解决方案来列出组及其分配的点,但我很难将结果存储回myTable,即UPDATE在桌子上做一个。我终于成功了(见帖子底部!!)。

但首先,这里是组得分表生成器(组概述):

SELECT mg,ms,mm,
  CASE WHEN @s=ms THEN 
    CASE WHEN @m=mm THEN @i 
         WHEN @i>2 THEN @i:=@i-2 
         ELSE null END
  ELSE @i:=5 END pt,
  @g:=mg gr,@s:=ms,@m:=mm 
FROM (
  SELECT group1 mg,section ms,max(marks) mm FROM mytable
  WHERE group1>''
  GROUP BY group1,section
) m 
ORDER BY ms,mm desc,mg

http://sqlfiddle.com/#!2/bea2a2/1

它给了我这个列表:

|  MG |     MS | MM |     PT |  GR | @S:=MS | @M:=MM |
------------------------------------------------------
|  G1 | class1 | 55 |      5 |  G1 | class1 |     55 |
| G10 | class1 | 55 |      5 | G10 | class1 |     55 |
|  G5 | class1 | 32 |      3 |  G5 | class1 |     32 |
|  G8 | class1 | 22 |      1 |  G8 | class1 |     22 |
|  G9 | class1 | 20 | (null) |  G9 | class1 |     20 |
| G66 | class2 | 66 |      5 | G66 | class2 |     66 |
| G88 | class2 | 65 |      3 | G88 | class2 |     65 |
|  G4 | class2 | 60 |      1 |  G4 | class2 |     60 |
| G55 | class2 | 33 | (null) | G55 | class2 |     33 |

我回来了(26.08.2013,在自己寻求帮助后,请参阅此处),现在可以提供完整的答案

SET @s:=@m:=@i:='a'; -- variables *MUST* be "declared" in some
                     -- way, otherwise UPDATE will not work!

UPDATE mytable INNER JOIN 
(SELECT mg,ms,mm,
   CASE WHEN @s=ms THEN 
     CASE WHEN @m=mm THEN @i 
          WHEN @i>2 THEN @i:=@i-2 
          ELSE null END
   ELSE @i:=5 END pt,
   @s:=ms,@m:=mm 
 FROM (
   SELECT group1 mg,section ms,max(marks) mm FROM mytable
   WHERE group1>''
   GROUP BY group1,section
 ) m 
 ORDER BY ms,mm desc,mg
) t ON mg=group1 AND ms=section AND mm=marks
SET Points=pt

看到这里http://sqlfiddle.com/#!2/bb7f2

最后 - 题外话:

亲爱的用户@user2594154,你为什么用同样的问题轰炸这个板8次?!?

  • 在最高分上添加特定点 查找点和分组的总和
  • 最高分的前三组应该有具体的分数(这篇文章
  • 按名称和点对结果进行分组 按点和点对每个部分进行分组
  • CompetitionName 降序查找两个学生的最高点
  • 在一个部分显示 3 列的最大值,包括重复
  • 具有条件的值前三个最高分的组应该
  • 有具体点 5,3, 1 [重复]

如果您将问题保留在一篇文章中,准确解释您想要什么以及您自己尝试过什么(!!),这对每个人都会更有帮助。然后,在回答过程中,可以对其进行编辑,使其更加精确。仅应发布新问题,如果它们的主题实际上不同,请参见此处

没有难过的感觉——在解决你的这个问题的过程中,我学到了很多东西。;-)

于 2013-08-23T17:28:42.123 回答
0

你可以做一个更新加入来实现你想要的;

UPDATE students
JOIN (
  SELECT marks, (@sp:=@sp-2) a
  FROM (
    SELECT distinct marks FROM students
    WHERE section='class1' AND group1 IS NOT NULL
    GROUP BY group1 ORDER BY marks DESC LIMIT 3
  ) b, (SELECT @sp:=7) c
) d
SET students.points = d.a 
WHERE students.marks = d.marks
  AND section='class1'
  AND group1 IS NOT NULL;

(也将此答案发布到您的第二个问题,您似乎要求更新声明而没有注意到它是重复的,将其移至此处)

一个用于测试的 SQLfiddle

于 2013-08-24T08:17:56.517 回答