IFNOTOBJECT_ID('[t_DemoA]') ISNULL
DROPTABLE[t_DemoA]
GO
CREATETABLE dbo.[t_DemoA] (
ID intNOTNULLIDENTITY (1, 1),
Y INTNOTNULL,
X Nvarchar(10) NULL
)
GO
--插入数据
INSERTINTO[t_DemoA](Y, X) values(2,'a')
INSERTINTO[t_DemoA](Y, X) values(2,'a')
INSERTINTO[t_DemoA](Y, X) values(5,'b')
INSERTINTO[t_DemoA](Y, X) values(2,'a')
INSERTINTO[t_DemoA](Y, X) values(2,'b')
INSERTINTO[t_DemoA](Y, X) values(5,'a')
INSERTINTO[t_DemoA](Y, X) values(5,'c')
GO
SELECT*FROM[t_DemoA] T
WHERE ID in (
SELECTtop2 ID FROM[t_DemoA]
WHERE x=T.X orderby ID asc )
/*
ID Y X
1 2 a
2 2 a
3 5 b
5 2 b
7 5 c
*/