SQL筆試題 取code相同的,value最大的記錄

[table]
|id|code|value|
|1|a|1|
|2|a|3|
|3|a|3|
|4|b|3|
|5|b|4|
|6|b|5|
[/table]

取code相同的,value最大的記錄,value相同時,取id值大的

select * from test t1
where not exists (select * from test t2 where t1.code = t2.code and t1.value < t2.value)
and not exists (select * from test t3 where t1.code = t3.code and t1.value = t3.value and t1.id < t3.id)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章