select m.id, m.a, n.id, n.afrom(select t0.* from t t0where not exists (select 1 from t t1 where t1.sj = t0.sj and t1.a > t0.a)) mselect t0.* from t t0 where not exists (select 1 from t t1 where t1.sj = t0.sj and t1.a < t0.a)) nwhere m.sj = n.sj;