SQL Quiz 找出欄位中最大值

2021-08-15 07:35:28 字數 1633 閱讀 1036

內部訓練題目

最近在幫部門內的同仁快快樂樂學t-sql,想到乙個好玩的題目給大家,假設有乙個資料表students,裡面記錄學生的三次考試成績,需要找到三次中最高的成績,請問要如何下sql 呢 ?

這個是產生測試範例

希望大家可以寫超過三種以上的解法

1 使用case 的作法( 如果要判斷的字段不多是還算簡單 )

select name, 

case

when ( score1 >= score2 ) and ( score1 >= score3 ) then score1

when ( score2 >= score1 ) and ( score2 >= score3 ) then score2

when ( score3 >= score1 ) and ( score3 >= score2 ) then score3

endas maxscore

from

#students

2 在select 使用values 來轉換,我比較喜歡的做法,但可能有些人不習慣這樣的寫法 )

select name , 

( select

max(score)

from ( values (score1),(score2),(score3)) t(score) ) as maxscore

from #students

3 利用union 將資料切分多筆,人工作unpivot 的處理

select name, max(score) maxscore from

( select name, score1 as score

from #students

union

select name, score2

from #students

union

select name, score3

from #students

) students(name, score)

group

by name

4 跟3 的做法觀念相同,但改用unpivot 的指令來處理

select name, max(score) as maxscore 

from #students

unpivot ( score for dateval in ( score1, score2, score3 ) ) as scoretable

group

by name

求陣列中最大值

所有c語言 都是在loadrunner中執行 action 定義乙個int陣列 int len 記錄陣列元素個數 int max 所求的最大值 int i 迴圈變數 loadrunnerg中,不能在for迴圈中定義變數 len sizeof a sizeof int 陣列元素個數 陣列所佔字元數 陣...

最大值 python找出幾個數最大值的方法

python找出幾個數中最大值的方法 1 簡單的使用if else進行判斷 list 12,34,2,0,1 max list 0 定義變數max用來儲存最大值,初始值賦值為列表中任意乙個值 for i in list if i max max i print 這個列表中最大值為 max 2 使用m...

python獲取序列中最大值

test 1,2,3 4,5,6 7,8,9 這個就可以看做是二維陣列了,直接建立 print test print test 1 這裡會輸出 4,5,6 print test 1 這裡會輸出 4,5,6 print test 1 這裡會輸出 4,5,6 print test 1 1 這裡輸出的是5...