-4

我的桌子

+------+--------+---------+-------+--------+
| NAME | GROUP1 | SECTION | MARKS | POINTS |
+------+--------+---------+-------+--------+
| S1   | G1     | class1  |    55 | (null) |
| S16  | G1     | class1  |    55 | (null) |
| S17  | G1     | class1  |    55 | (null) |

| S2   | (null) | class2  |    33 | (null) |

| S25  | G10    | class1  |    55 | (null) |
| S26  | G10    | class1  |    55 | (null) |

| S4   | G88    | class2  |    65 | (null) |
| S5   | G88    | class2  |    65 | (null) |
| S32  | (null) | class1  |    65 | (null) |

| S7   | G5     | class1  |    32 | (null) |
| S18  | G5     | class1  |    32 | (null) |

| S10  | (null) | class2  |    78 | (null) |

| S8   | G8     | class1  |    22 | (null) |
| S20  | G8     | class1  |    22 | (null) |

| S22  | G9     | class1  |    20 | (null) |
| S23  | G9     | class1  |    20 | (null) |

| S13  | G55    | class2  |    33 | (null) |
| S36  | G55    | class2  |    33 | (null) |
+------+--------+---------+-------+--------+

SQL FIDDLE:http ://www.sqlfiddle.com/#!2/f4b54/1/0

我之前提出过这个问题,根据评论我试图更清楚地改进这个问题。

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

例如:在 class1 - 组“G1”和“G10”得到“55”,他们在“class1”中获得最高分。所以我想给'G1'和'G10'组的所有学生'5'分。

G5组得了第二高分,所以该组的所有学生都应该给3分。同样的方式 G8 - 1 分。

请注意:任何没有团体 ID 的学生参加单项,不考虑该记录。

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

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

我的最终输出寻找一个部分。请只给一个部分(class1)

+------+--------+---------+-------+--------+
| NAME | GROUP1 | SECTION | MARKS | POINTS |
+------+--------+---------+-------+--------+
| S1   | G1     | class1  |    55 |    5   |
| S16  | G1     | class1  |    55 |    5   |
| S17  | G1     | class1  |    55 |    5   |

| S2   | (null) | class2  |    33 | (null) |

| S25  | G10    | class1  |    55 |    5   |
| S26  | G10    | class1  |    55 |    5   |

| S4   | G88    | class2  |    65 | (null) |
| S5   | G88    | class2  |    65 | (null) |

| S32  | (null) | class1  |    65 | (null) |

| S7   | G5     | class1  |    32 |    3   |
| S18  | G5     | class1  |    32 |    3   |

| S10  | (null) | class2  |    78 | (null) |

| S8   | G8     | class1  |    22 |    1   |
| S20  | G8     | class1  |    22 |    1   |

| S22  | G9     | class1  |    20 | (null) |
| S23  | G9     | class1  |    20 | (null) |

| S13  | G55    | class2  |    33 | (null) |
| S36  | G55    | class2  |    33 | (null) |
+------+--------+---------+-------+--------+

请帮助我..

4

1 回答 1

-1

这个答案是Itay给出的

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

FROM   `students` t1

LEFT JOIN (
    (
        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`.`avg`,
                      CASE @curRow := @curRow + 1  
                           WHEN '1' THEN 5
                           WHEN '2' THEN 3
                           WHEN '3' THEN 1
                           ELSE NULL END 'points'

              FROM (SELECT `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`
                    GROUP BY `t_avg`.`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-24T06:53:35.400 回答