2016-10-13 6 views
0

tbl_marks:aus verschiedenen Tabellen

+-------+-----------+-----------+-----------+-----------+----------+ 
|examid | section1 | section2 | section3 | section4 | year | 
+-------+-----------+-----------+-----------+-----------+----------+ 
|E1  |  100 |  101 |  102 |  103 | 2016 | 
|E2  |  200 |  201 |  202 |  203 | 2015 | 
|E3  |  300 |  301 |  302 |  303 | 2014 | 
|E4  |   |   |   |   | 2013 | 
|E5  |  400 |  401 |  402 |  403 | 2016 | 
|E6  |  500 |  501 |  502 |  503 | 2015 | 
|E7  |  600 |  601 |  602 |  603 | 2014 | 
|E8  |   |  701 |   |  703 | 2013 | 
+-------+-----------+-----------+-----------+-----------+----------+ 

tbl_student:

+-------+-----+---------+ 
|name | sid | rollnum | 
+-------+-----+---------+ 
|cheery | 1 |  X1 | 
|apple | 2 |  X2 | 
+-------+-----+---------+ 

tbl_exam:

+--------+------+ 
|examnum | sid | 
+--------+------+ 
|  E1 | 1 | 
|  E2 | 1 | 
|  E3 | 1 | 
|  E4 | 1 | 
|  E5 | 2 | 
|  E6 | 2 | 
|  E7 | 2 | 
|  E8 | 2 | 
+--------+------+ 

Erwartete Ausgabe:

Section1, 2, 3, 4 stellen Scores von Se dar Prüfung 1, 2, 3, 4 einer Prüfung. Bitte beachten Sie, dass, wenn die Punktzahl eines Abschnitts null ist, sollte es durch Nullen (000) ersetzt werden.

Endgültige Ausgabetabelle hat Spalte - Scores, die Ergebnis der Scores aller Abschnitte eines bestimmten Jahres verkettet ist. score1 verkettet Ergebnis aller Abschnitte Scores von Jahr 2016. Ähnlich score2 - 2015, score3 - 2014 und etc.

Die Ausgabe sollte 2 Zeilen für zwei Personen mit 5 Spalten haben - roll num, verkettete Abschnitt Scores von 2016, 2015, 2014 und 2013.

+------+------------+-------------+-------------+-------------+ 
| num | score1 | score2  | score3  | score4 | 
+------+------------+-------------+-------------+-------------+ 
| X1 |100101102103| 200201202203| 300301302303| 000000000000| 
| X2 |400401402403| 500501502503| 600601602603| 000702000703| 
+------+------------+-------------+-------------+-------------+ 

Irgendwelche Vorschläge werden geschätzt. Vielen Dank.

+0

@Jens Vielen Dank für Ihre Hilfe beim Formatieren der Tabelle. Bin dankbar. Ich habe versucht, Formatregeln zu folgen. Ich komme nicht dahin, wo ich falsch liege. – newuser

+0

Encapsulate sie in Codeblöcken. – Jens

+0

Sorry, aber ich verstehe nicht, was ist section1, section2, etc ... –

Antwort

0

1. Schritt: Registriert

SELECT * 
FROM tbl_student t_s 
LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum 

2. Schritt: Wählen Sie mit case/wenn

SELECT t_s.rollnum as "num", 
     CASE 
      WHEN t_m.year = '2016' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END AS score1, 
     CASE 
      WHEN t_m.year = '2015' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END AS score2, 
     CASE 
      WHEN t_m.year = '2014' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END AS score3, 
     CASE 
      WHEN t_m.year = '2013' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END AS score4 
    FROM tbl_student t_s 
    LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
    LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum 

Wir sind in die richtige Richtung bekommen hier.

3. Schritt: MAX & GROUP BY

Wir brauchen alle das Ergebnis von rollnum zu gruppieren, so dass alle anderen ausgewählten Spalte müssen Aggregatfunktionen sein.

SELECT t_s.rollnum as "num", 
     MAX(CASE 
      WHEN t_m.year = '2016' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END AS) score1, 
     MAX(CASE 
      WHEN t_m.year = '2015' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END) AS score2, 
     MAX(CASE 
      WHEN t_m.year = '2014' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END) AS score3, 
     MAX(CASE 
      WHEN t_m.year = '2013' THEN CONCAT_WS('',t_m.section1, t_m.section2, t_m.section3, t_m.section4) 
     END) AS score4 
    FROM tbl_student t_s 
    LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
    LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum 
    GROUP BY num 

Letzter Schritt: Coalesce

SELECT t_s.rollnum as "num", 
     MAX(CASE 
      WHEN t_m.year = '2016' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000')) 
     END AS) score1, 
     MAX(CASE 
      WHEN t_m.year = '2015' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000')) 
     END) AS score2, 
     MAX(CASE 
      WHEN t_m.year = '2014' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000')) 
     END) AS score3, 
     MAX(CASE 
      WHEN t_m.year = '2013' THEN CONCAT_WS('',COALESCE(t_m.section1,'000'), COALESCE(t_m.section2,'000'), COALESCE(t_m.section3,'000'), COALESCE(t_m.section4,'000')) 
     END) AS score4 
    FROM tbl_student t_s 
    LEFT JOIN tbl_exam t_e ON t_e.sid = t_s.sid 
    LEFT JOIN tbl_marks t_m ON t_m.examid = t_e.examnum 
    GROUP BY num 

Damit Sie sollte das Ergebnis zu erwarten, kann ich nicht testen, so kann ich einige Fehler gemacht haben, um einen Kommentar hinterlassen, wenn es doesn t arbeiten.

Verwandte Themen