したがって、ROW_NUMBER 関数を使用します。
DECLARE @TopN INT;
SET @TopN = 3;
SELECT ...
FROM
(
SELECT ...,
RowNum = ROW_NUMBER() OVER(PARTITION BY t.Age ORDER BY t.LastName, t.FirstName)
FROM MySchema.MyTable AS t
) src
WHERE src.RowNum <= @TopN
AdventureWorks データベースがインストールされている場合(私は AdventureWorks2008 を使用しました)、次のスクリプトをテストに使用できます。
-- Because Person.Person table doesn't has an `Age` column
-- I create a new table (dbo.Person) having following columns:
-- BusinessEntityID, LastName, FirstName and Age columns
SELECT p.BusinessEntityID, p.LastName, p.FirstName,
1 + ABS(CHECKSUM(NEWID())) % 100 AS Age
INTO dbo.Persons
FROM Person.Person p;
GO
/*
ALTER TABLE dbo.Persons
ADD CONSTRAINT PK_Persons_BusinessEntityID
PRIMARY KEY (BusinessEntityID)
*/
DECLARE @TopN INT;
SET @TopN = 3;
SELECT src.BusinessEntityID, src.LastName, src.FirstName, src.Age, src.RowNum
FROM
(
SELECT p.BusinessEntityID, p.LastName, p.FirstName, p.Age,
RowNum = ROW_NUMBER() OVER(PARTITION BY p.Age ORDER BY p.LastName, p.FirstName)
FROM dbo.Persons AS p
) src
WHERE src.RowNum <= @TopN
ORDER BY src.Age, src.LastName, src.FirstName;
-- DROP TABLE dbo.Persons
結果:
BusinessEntityID LastName FirstName Age RowNum
---------------- --------- ---------- --- ------
...
10905 Allen Kaitlyn 30 1
15052 Alonso Gina 30 2
5505 Alonso Jessie 30 3
20216 Alexander Alyssa 31 1
3789 Allen Wyatt 31 2
2798 Alonso Alfredo 31 3
16850 Adams Gabriel 32 1
4747 Adams Ian 32 2
7761 Alexander Jacqueline 32 3
...