Instead of hardwiring the business logic in the query, add two columns to your Certifications table.One column Grp to hold together certifications in group. And one column denoting ranking within group.The the query can look like thisDECLARE @Tests TABLE ( ID INT, Student VARCHAR(20), CertID INT )INSERT @TestsSELECT 6, 'Peso', 1 UNION ALLSELECT 7, 'Peso', 3 UNION ALLSELECT 1, 'John', 1 UNION ALLSELECT 2, 'John', 2 UNION ALLSELECT 3, 'Tom', 2 UNION ALLSELECT 4, 'Tom', 3 UNION ALLSELECT 5, 'John', 3DECLARE @Certs TABLE ( CertTypeID INT, CertTypeName VARCHAR(20), Score INT, Grp INT, Rank INT )INSERT @CertsSELECT 1, 'ECDL', 45, 1, 2 UNION ALLSELECT 2, 'ECDL Advanced', 90, 1, 1 UNION ALLSELECT 3, 'Java Course', 10, 2, 1SELECT Student, SUM(Score) AS TotalScoreFROM ( SELECT t.Student, c.Score, ROW_NUMBER() OVER (PARTITION BY t.Student, c.Grp ORDER BY c.RANK) AS recID FROM @Tests AS t INNER JOIN @Certs AS c ON c.CertTypeID = t.CertID ) AS dWHERE recID = 1GROUP BY StudentORDER BY Student
N 56°04'39.26"E 12°55'05.63"