Kodewick
開始學 →
基礎←→0 / 70

分數換等級

#056基礎CASE WHEN … WHEN約 4 分鐘

老師想把數學分數換成等級:90 及以上 'A',75 及以上 'B',60 及以上 'C',其他 'D'。從 scores 裡選出數學(subject 是 'math')的 student_id、score 和等級三列。

用到的表
students
  • idPK
  • name
  • class
  • age
  • city
scores
  • idPK
  • student_idFK
  • subject
  • score

scores.student_id → students.id

students
idnameclassagecity
1Amy1A12Leeds
2Ben1A13York
3Cara1B12NULL
4Dan1B14Leeds
5Eva1A12Hull
6Finn1C13NULL
7Gus1C12York
7 行
scores
idstudent_idsubjectscore
11math92
21art78
32math65
42art88
53math74
64math55
74art90
85math81
95artNULL
107math99
10 行
query.sql
SELECT student_id, score,
  CASE
    -- 在這裡寫:從高到低三個 WHEN(90、75、60),再 ELSE 'D'
  END
FROM scores
WHERE subject = 'math';
Ctrl + Enter 也能執行
結果

寫好了先點「執行」看看結果,沒問題再點「提交」。