sql語句:取按a列分組後,b列最大,的所有列的記錄
阿新 • • 發佈:2019-02-13
例如:
a | b | c |
1 | 5 | abc |
2 | 6 | bcd |
1 | 7 | ade |
2 | 8 | adc |
若取按a列分組後,b列最大,的所有列的記錄,
a | b | c |
1 | 6 | bcd |
2 | 8 | adc |
可以使用如下語句
select * from test where b in ( select max (id) from test group by a)適用於所有資料庫
select t1.a,t1.b,t1.cfrom test t1
inner join
(seelct a, max (b) as b from test group by a) t2
on t1.a = t2.a and t1.b = t2.b
適用於所有資料庫
select a,b,cfrom (
select a,b,c
,row_number() over (partition by a order by b desc ) rn
)
where rn = 1
適用於oracle