2013-08-14 17 views
5

mein TischFinden Summe der Punkte und die Gruppierung

Referenz: http://www.sqlfiddle.com/#!2/6be93/1

enter image description here

Hier würde Ich mag Gesamtpunkt einer Schule finden. Ich verwende folgenden Code.

SELECT School, SUM(GroupPoint) AS TotalC1, SUM(C2Points) AS TotalC2, 
    SUM(C3Points) AS TotalC3, SUM(GroupPoint + C2Points + C3Points) AS TotalAll 
    FROM students GROUP BY School ORDER BY TotalAll DESC LIMIT 6 

Referenz: http://www.sqlfiddle.com/#!2/25ed3/2

mein Problem, die ID-1,2,3 sind die Gewinner eines Gruppenwettbewerb. Sie erhalten also 5 Punkte individuell. Aber für diesen Wettbewerb bekommt die SCHULE nur 5 Punkte, nicht 15. Eine Gruppe kann mit demselben ChessNO identifiziert werden.

Also meine letzte Ausgabe sieht

SCHOOL TOTALC1 TOTALC2 TOTALC3 TOTALALL 
School2 13  49  3  65  
School1 5  4  25  34 

Es wäre dankbar, wenn mir jemand helfen könnte,

Antwort

2

Ofcourse, Sie einige Optimierungen tun können. Aber es funktioniert!

SELECT two.TOTALC1, one.TotalC2, one.TotalC3, one.TotalOne + two.TOTALC1 as TotalAll from 
(select School, 
    SUM(C2Points) AS TotalC2, 
    SUM(C3Points) AS TotalC3, 
    SUM(C2Points + C3Points) AS TotalOne 
FROM students GROUP BY School 
ORDER BY TotalOne DESC) one 
left join (select school, sum(ma) as TOTALC1 from (select school, chess, max(grouppoint) as ma from students group by school, chess) as b group by school) two 
on one.school = two.school 
0
SELECT School, SUM(GroupPoint) AS TotalC1, SUM(C2Points) AS TotalC2, 
    SUM(C3Points) AS TotalC3, SUM(GroupPoint + C2Points + C3Points) AS TotalAll 
    FROM students GROUP BY Chess ORDER BY TotalAll DESC LIMIT 6 
+0

dieser Code mein Freund nicht funktioniert. Trotzdem danke für deine Mühe. – user2594154

2

try this

SELECT 
    School, 
    sum(GroupPoint), 
    sum(TotalC2), 
    sum(TotalC3), 
    sum(GroupPoint) + sum(TotalC2) + sum(TotalC3) as total 
FROM (
    SELECT 
    School, 
    MAX( GroupPoint) AS GroupPoint, 
    SUM( C2Points) AS TotalC2, 
    SUM( C3Points) AS TotalC3 
    FROM 
    students 
    GROUP BY 
    School, 
    Chess 
) subquery 
GROUP BY 
    School 

Ausgabe

| SCHOOL | SUM(GROUPPOINT) | SUM(TOTALC2) | SUM(TOTALC3) | TOTAL | 
------------------------------------------------------------------- 
| School1 |    5 |   4 |   25 | 34 | 
| School2 |    13 |   49 |   3 | 65 | 
Verwandte Themen