简体   繁体   中英

Picking out and showing only the highest value from one column based on three columns in MySQL

Hei!

I have searched everywhere but cannot find the correct way to select only the highest result per student inside the same topic. So studentnr is the ID of the student in the table, emnekode is the topic code and karakter is result with A as highest and F as lowest.

Any suggestions to how I can make it work? I would appreciate it.

Here is the code so far with output under:

SELECT Eksamensresultat.*, Emnenavn, Studiepoeng
FROM Eksamensresultat, Emne
WHERE Eksamensresultat.Emnekode = Emne.Emnekode 
ORDER BY RIGHT (Eksamensresultat.Emnekode, 4) ASC;

I want the output to be like this (except result B since A is higher for that student):

输出(图片)

Here is the two tables:

CREATE TABLE Emne
(
Emnekode CHAR(8) NOT NULL,
Emnenavn CHAR(40) NOT NULL,
Studiepoeng DECIMAL(3, 1),
CONSTRAINT EmnekodePK PRIMARY KEY(Emnekode)
);
CREATE TABLE Eksamen
(
Emnekode CHAR(8) NOT NULL,
Dato DATE NOT NULL,
Romnr CHAR(4) NOT NULL,
CONSTRAINT EksamenPK PRIMARY KEY(Dato,Emnekode),
CONSTRAINT EksamenEmneFK FOREIGN KEY(Emnekode) REFERENCES Emne(Emnekode),
CONSTRAINT EksamenRomFK FOREIGN KEY(Romnr) REFERENCES Rom(Romnr)
);

Data:

INSERT INTO Emne (Emnekode, Emnenavn, Studiepoeng) VALUES
("PRG1000", "Grunnleggende programmering 1", 7.5),
("PRG1100", "Grunnleggende programmering 2", 7.5),
("WEB1100", "Webutvikling og HCI", 7.5),
("SYS1000", "Systemutvikling", 7.5),
("ORL1100", "Organisering", 7.5);
INSERT INTO Eksamensresultat(Karakter,Studentnr,Emnekode,Dato) VALUES
("A","240202","PRG1000","20210505"),
("C","240202","PRG1100","20210506"),
("B","240202","SYS1000","20210507"),
("A","225087","PRG1100","20210506"),
(NULL,"225087","SYS1000","20210507"),
(NULL,"240225","SYS1000","20210507"),
(NULL,"884642","SYS1000","20210507"),
("C","139959","PRG1000","20210505"),
("B","240202","PRG1000","20210606");

One way is to identify the right row:

SELECT Studentnr, Emnekode, min(Karakter)
FROM Eksamensresultat
GROUP BY 1, 2

then join that with your query to select the correct row of Eksamensresultat (only the group by column and aggregate are valid):

SELECT Eksamensresultat.*, Emnenavn, Studiepoeng
FROM Eksamensresultat as e
JOIN (
  SELECT Studentnr, Emnekode, min(Karakter) 'Karakter'
  FROM Eksamensresultat
  GROUP BY 1, 2
) using (Studentnr, Emnekode, Karakter)
JOIN Emne using (Emnekode)
ORDER BY RIGHT (Eksamensresultat.Emnekode, 4) ASC;

The other (more readable) option is to use a window function if your mysql is recent enough. See https://dev.mysql.com/doc/refman/8.0/en/window-functions-usage.html . I would need a working schema to test that query for you.

If I understand correctly, you want ROW_NUMBER() , so you can choose one row per student/exam with the higher Karakter :

SELECT er.*
FROM (SELECT er.*, e.Emnenavn, e.Studiepoeng,
             ROW_NUMBER() OVER (PARTITION BY er.Studentnr, er.Emnekode ORDER BY er.Karakter DESC) as seqnum
      FROM Eksamensresultat er JOIN
           Emne e
           ON er.Emnekode = e.Emnekode 
     ) er
WHERE er = 1
ORDER BY RIGHT(Emnekode, 4) ASC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM