0

我有桌子

id    code    name
=====================
1     30100   John
2     30100   Andrew
3     30100   Sandy
4     29145   Mike
5     29145   Tony
6     29145   Laura
7     29145   Henry
8     00124   Michael
9     00124   Teddy
10    13405   Andy
11    09325   Patrick

我只想选择按代码分组的 2 个名称并获得此结果。

id    code    name
=====================
1     30100   John
2     30100   Andrew
4     29145   Mike
5     29145   Tony
8     00124   Michael
9     00124   Teddy
10    13405   Andy
11    09325   Patrick

有人可以帮我做这样的查询吗?

谢谢

4

1 回答 1

1
SELECT id, code, name
FROM   TableName a
WHERE
(
   SELECT count(*) 
   FROM   TableName as f
   WHERE  f.code = a.code and a.id >= f.id
) <= 2
ORDER BY id, code, name 
于 2013-10-06T11:04:56.990 回答