Вот рабочее решение:
WITH ExamResults AS
(
SELECT 1 ResultId, 'Joe Blow' Student, 'A-' Grade
UNION ALL SELECT 2, 'Leroy Jones', 'B+'
UNION ALL SELECT 3, 'Paul Smith', 'B-'
UNION ALL SELECT 4, 'Helen Potter', 'B'
),
Suffix AS
(
SELECT '+' Suffix, 1 [Rank]
UNION ALL SELECT '', 2
UNION ALL SELECT '-', 3
),
Grade AS
(
SELECT DISTINCT Grade, LEFT(Grade, 1) Letter, SUBSTRING(Grade, 2, 1) Suffix
FROM ExamResults
)
SELECT ExamResults.*,
Grade.Letter,
Grade.Suffix,
Grade.Letter + CAST(Suffix.[Rank] AS char(1)) GradeOrder
FROM ExamResults
JOIN Grade ON Grade.Grade = ExamResults.Grade
JOIN Suffix ON Suffix.Suffix = Grade.Suffix
ORDER BY
Grade.Letter,
Suffix.[Rank]
Вывод:
ResultId Student Grade Letter Suffix GradeOrder
----------- ------------ ----- ------ ------ ----------
1 Joe Blow A- A - A3
2 Leroy Jones B+ B + B1
4 Helen Potter B B B2
3 Paul Smith B- B - B3