数据
+----+-------+------+--------+----+----+----------+------------+----------+----------+
| ID | CHESS | NAME | GROUPC | C2 | C3 | SECTION | GROUPPOINT | C2POINTS | C3POINTS |
+----+-------+------+--------+----+----+----------+------------+----------+----------+
| 1 | C100 | S1 | 1 | 0 | 1 | Section1 | 5 | 0 | 3 |
| 2 | C100 | S2 | 1 | 0 | 1 | Section1 | 5 | 0 | 5 |
| 3 | C100 | S3 | 1 | 1 | 1 | Section1 | 5 | 5 | 1 |
| 4 | C101 | S4 | 0 | 1 | 0 | Section3 | 0 | 3 | 0 |
| 5 | C102 | S5 | 0 | 1 | 0 | Section1 | 0 | 3 | 0 |
| 6 | C103 | S6 | 1 | 1 | 5 | Section2 | 1 | 1 | 0 |
| 7 | C103 | S7 | 1 | 1 | 0 | Section2 | 1 | 0 | 0 |
| 8 | C103 | S8 | 1 | 0 | 3 | Section2 | 1 | 0 | 5 |
| 9 | C105 | S9 | 0 | 0 | 0 | Section1 | 0 | 0 | 0 |
| 10 | C104 | S10 | 1 | 1 | 0 | Section3 | 3 | 3 | 0 |
| 11 | C104 | S11 | 1 | 0 | 0 | Section3 | 3 | 0 | 0 |
| 12 | C104 | S12 | 1 | 1 | 1 | Section3 | 3 | 1 | 3 |
| 13 | C107 | S13 | 1 | 1 | 1 | Section1 | 3 | 1 | 1 |
| 14 | C107 | S14 | 1 | 1 | 1 | Section1 | 3 | 1 | 1 |
| 15 | C107 | S15 | 1 | 1 | 1 | Section1 | 3 | 1 | 1 |
| 16 | C105 | S16 | 1 | 1 | 0 | Section3 | 0 | 5 | 0 |
| 17 | C105 | S17 | 1 | 1 | 0 | Section3 | 0 | 5 | 0 |
| 18 | C105 | S18 | 1 | 1 | 0 | Section3 | 0 | 5 | 0 |
| 19 | C109 | S19 | 0 | 1 | 0 | Section3 | 1 | 0 | 0 |
+----+-------+------+--------+----+----+----------+------------+----------+----------+
问题
我想找到一个部分的总分。如果 forChess
的值相同:Name
和GroupPoint
/ C2Points
/ C3Points
(取决于GROUPC
/ C2
/ C3
)应该被分组。
我目前正在使用以下查询,但不知道如何按Name
and进行分组GroupPoints
:
select * from ( select s1.ID, s1.Name, 'Competition 1' as Competition,
s1.GroupPoint as Points from students s1 where SECTION='Section1' and
ifnull(s1.GROUPC,'')<>'' and s1.GroupPoint<>0
union
select s2.ID, s2.Name, 'Competition 2' as Competition, s2.C2Points as Points
from students s2 where s2.SECTION='Section1' and ifnull(s2.C2,'')<>'' and
s2.C2Points<>0
union
select s3.ID, s3.Name, 'Competition 3' as Competition, s3.C3Points as Points
from students s3 where s3.SECTION='Section1' and ifnull(s3.C2,'')<>'' and 3.C3Points<>0
)t order by t.points desc, t.Competition ASC
请参考这个SqlFiddle
首选输出
问题是:如何按名称/点对结果进行分组,以便最终输出类似于:
+----------+---------------+--+--------------+--------+
| Section1 | | | | |
+----------+---------------+--+--------------+--------+
| ID | NAME | | COMPETITION | POINTS |
| 1 | S1, S2, S3 | | Competition1 | 5 |
| 3 | S3 | | Competition2 | 5 |
| 2 | S2 | | Competition3 | 5 |
| 15 | S13, S14, S15 | | Competition1 | 3 |
| 5 | S5 | | Competition2 | 3 |
| 1 | S1 | | Competition3 | 3 |
| 15 | S15 | | Competition2 | 1 |
| 13 | S13 | | Competition2 | 1 |
| 14 | S14 | | Competition2 | 1 |
| 14 | S14 | | Competition3 | 1 |
| 15 | S15 | | Competition3 | 1 |
+----------+---------------+--+--------------+--------+