表TB中,在gid为1和2中,要把souceid和type同时相同的找出来,只取一条
id souceid type gid
1 s1 t1 1
2 s1 t1 2
3 s2 t2 1
4 s2 t2 2
5 s3 t3 1
我了解的三种方法分别利用in、join、row_number
1.select * from TB where id in (select Max(id) from TB where gid in(1,2) group by souceid,type)
2.select * from TB A join (select Max(id) id from TB where gid in(1,2) group by souceid,type) B on A.id=B.id
3.select * from (select id,souceid,type,gid,row_number() over (partition by souceid,type order by id) as rn) A where A.rn=1
请高手指正,哪种方法最高效亦或是有更好的改进方法
眼眸繁星
撒科打诨
UYOU
相关分类