每个类型仅显示 2 个
如何用一句 sql 提取表内容,每个类型仅显示 2 个
如表 s
id name t
-------------------------------
1 aa 1
2 bb 1
3 cc 1
4 dd 2
5 ee 2
6 ff 2
7 gg 3
8 hh 3
9 ii 3
求结果表 s
id name t
------------------------------
1 aa 1
2 bb 1
4 dd 2
5 ee 2
7 gg 3
8 hh 3
谢谢了~~~~~~~~~~~~~~
------解决方案----------------------方法一:
Select * From s A
Where (Select Count(t) From s Where t = A.t And id < A.id) < 2
Order By t, id
--方法二:
Select * From s A
Where Exists (Select Count(t) From s Where t = A.t And id < A.id Having Count(t) < 2)
Order By t, id
--方法三:
Select * From s A
Where id In (Select TOP 2 id From s Where t = A.t Order By id)
Order By t, id
------解决方案--------------------select * from s
where id in (select top 2 id from s as s2 where s2.t = s.t)