2017-02-07 5 views
2

Dies ist meine erste Frage, so hoffe ich, dass ich es nicht vermasseln.Mysql Abfrage Summe eine andere Spalte mit einer Bedingung

Hier ist ein SQL Fiddle ich mich, es zu helfen, machte erklären: http://sqlfiddle.com/#!9/7f2b5c/7

Wir haben Fragen, Antworten, question_types und answer_types.

Ich möchte für jede Frage wissen, wie viele Leute jeden Antworttyp beantwortet haben und wie viele Leute diese Frage beantwortet haben, aber ich kann nicht die Summe erhalten.

Dies ist, was ich bekomme:

id | fk_question_type | description   | num | value  | total 
1 | 1    | How was the breakfast? | 0 | Bad  | 0 
1 | 1    | How was the breakfast? | 1 | Good  | 1 
1 | 1    | How was the breakfast? | 0 | Indifferent| 0 
1 | 1    | How was the breakfast? | 2 | Very good | 2 
2 | 1    | How was the lunch?  | 0 | Bad  | 0 
2 | 1    | How was the lunch?  | 1 | Good  | 1 
2 | 1    | How was the lunch?  | 0 | Indifferent| 1 
2 | 1    | How was the lunch?  | 1 | Very good | 1 

Dies ist, was Ich mag würde:

id | fk_question_type | description   | num | value  | total 
1 | 1    | How was the breakfast? | 0 | Bad  | 3 
1 | 1    | How was the breakfast? | 1 | Good  | 3 
1 | 1    | How was the breakfast? | 0 | Indifferent| 3 
1 | 1    | How was the breakfast? | 2 | Very good | 3 
2 | 1    | How was the lunch?  | 0 | Bad  | 2 
2 | 1    | How was the lunch?  | 1 | Good  | 2 
2 | 1    | How was the lunch?  | 0 | Indifferent| 2 
2 | 1    | How was the lunch?  | 1 | Very good | 2 

Die Abfrage ich jetzt bin versucht:

SELECT id, fk_question_type, description, num, value, SUM(num) AS totalAnswers 
FROM (
    (SELECT q.id, q.fk_question_type, q.description, COUNT(a.id) AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.id = a.`fk_answer_type` 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
    UNION ALL 
    (SELECT q.id, q.fk_question_type, q.description, 0 AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.fk_question_type=q.fk_question_type 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
) AS T 
WHERE fk_question_type = 1 
GROUP BY id, value ORDER BY id 

Wie kann ich die Nummer für die Zeilen mit der gleichen ID SUMMEN, während die anderen Spalten beibehalten werden?

+1

Müssen Sie verwenden 'LEFT JOIN' statt' INNER JOIN' wirklich? Kannst du wirklich eine Antwort ohne eine entsprechende Frage haben? – Barmar

+0

Aus der Tabelle "Antworten" in der Geige kann man sehen, dass die Fragen "Wie war das Frühstück?", "Wie war das Mittagessen?" Und "Würdest du wiederkommen?" Dreimal gefragt wurden. 'Wie war Abendessen' wurde nur zweimal gefragt. Könntest du bitte, was du in "num" und was in "total" sehen willst? –

+0

@DhruvSaxena Ich will 'num' sein, wie es schon ist,' Wie war das Frühstück? 'Wurde mit einem' Bad' Wert 0 mal, 1 mal als 'Gut', 0mal als' Indifferent' und 2mal als 'beantwortet Sehr gut. "Total" sollte sein, wie oft die Frage beantwortet wurde, also 3 Mal für jede Zeile von "Wie war das Frühstück?" Tut mir leid, wenn ich das nicht klar gemacht habe. – EnricSV

Antwort

0

Verwenden Sie eine Unterabfrage, um die Summe für jede id abzurufen, und verknüpfen Sie diese mit der Abfrage, die die Zeilen pro Wert erhält.

Und in der äußeren Abfrage, sollten Sie SUM(num) oder MAX(num) zu kombinieren, um die num Werte aus den beiden Abfragen in den UNION verwenden.

SELECT T.id, fk_question_type, description, SUM(num) AS num, value, T2.totalAnswers 
FROM (
    (SELECT q.id, q.fk_question_type, q.description, COUNT(a.id) AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.id = a.`fk_answer_type` 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
    UNION ALL 
    (SELECT q.id, q.fk_question_type, q.description, 0 AS num, at.value 
    FROM answer a 
    LEFT JOIN question q ON a.`fk_question`=q.`id` 
    LEFT JOIN answer_type at ON at.fk_question_type=q.fk_question_type 
    GROUP BY q.id, at.id ORDER BY q.id, at.id) 
) AS T 
JOIN (SELECT q.id, COUNT(*) AS totalAnswers 
     FROM answer a 
     LEFT JOIN question q ON a.`fk_question`=q.`id` 
     LEFT JOIN answer_type at ON at.id = a.`fk_answer_type` 
     GROUP BY q.id) AS t2 ON T.id = T2.id 
WHERE fk_question_type = 1 
GROUP BY T.id, value 

DEMO

+0

Danke! Ich habe deine Antwort angenommen. – EnricSV

Verwandte Themen