2

データ

+----+-------+------+--------+----+----+----------+------------+----------+----------+
| 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 |
+----+-------+------+--------+----+----+----------+------------+----------+----------+

問題

セクションの合計点を知りたいです。の値Chessが同じ場合:NameGroupPoint/ C2Points/ ( / /C3Pointsに応じて) をグループ化する必要があります。GROUPCC2C3

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      |
 +----------+---------------+--+--------------+--------+
4

1 に答える 1

1

追加GROUP BY Chess, GroupPointすると、次を使用して名前を連結できますGROUP_CONCAT()

あなたが望むことをする簡単な例を作成しました(union簡単にするために省略しました):

簡易クエリ

SELECT
  ID,
  GROUP_CONCAT(DISTINCT Name ORDER BY Name ASC SEPARATOR ", ") AS Name,
  "Competition 1" as Competition,
  GroupPoint AS Points
FROM students
WHERE Section = "Section1"
AND GROUPC IS NOT NULL
AND GroupPoint <> 0
GROUP BY Chess, GroupPoint

出力

+----+---------------+---------------+--------+
| ID | NAME          | COMPETITION   | POINTS |
+----+---------------+---------------+--------+
| 1  | S1, S2, S3    | Competition 1 | 5      |
| 13 | S13, S15, S15 | Competition 1 | 3      |
+----+---------------+---------------+--------+

sqlfiddleを参照してください

その他の注意事項

  1. SQL にエラーがあるようです: where you do はifnull(s3.C2,'')<>''、おそらく次のことを意味します:ifnull(s3.C3,'')<>''
  2. 正しいやり方ifnull(<field>, '')<>''<field> IS NOT NULL

最終クエリ

上記のすべてを考慮すると、最終的なクエリは次のようになるはずです。

SELECT *
FROM
(
SELECT s1.ID,
GROUP_CONCAT(s1.Name ORDER BY s1.Name ASC SEPARATOR ', ') AS Name,
'Competition 1' AS Competition,
s1.GroupPoint AS Points
FROM students s1
WHERE SECTION='Section1'
AND s1.GROUPC IS NOT NULL
AND s1.GroupPoint<>0
GROUP BY s1.Chess, s1.GroupPoint
UNION
SELECT s2.ID,
GROUP_CONCAT(s2.Name ORDER BY s2.Name ASC SEPARATOR ', ') AS Name,
'Competition 2' AS Competition,
s2.C2Points AS Points
FROM students s2
WHERE s2.SECTION='Section1'
AND s2.C2 IS NOT NULL
AND s2.C2Points<>0
GROUP BY s2.Chess, s2.C2Points
UNION
SELECT s3.ID,
GROUP_CONCAT(s3.Name ORDER BY s3.Name ASC SEPARATOR ', ') AS Name,
'Competition 3' AS Competition,
s3.C3Points AS Points
FROM students s3
WHERE s3.SECTION='Section1'
AND s3.C3 IS NOT NULL
AND s3.C3Points<>0
GROUP BY s3.Chess, s3.C3Points
)t
ORDER BY t.points DESC, t.Competition ASC

sqlfiddleを参照してください

于 2013-08-16T07:33:04.327 に答える