为列的每个唯一值选择最多N行

时间:2013-10-19 19:18:56

标签: sql sql-server tsql

我有一个包含下一列的表格:

First Name,
Last Name,
Age

让我们假设我们有

  • 2人年龄= 25
  • 年龄= 26
  • 的6人
  • 年龄= 27
  • 的10人

我想获得记录集,每个年龄我将拥有多达N条记录。 (记录可以是随机的)

你可以建议吗?

例如,如果N = 3,那么我们将

2 records with age = 25
3 records with age = 26
3 records with age = 27

2 个答案:

答案 0 :(得分:5)

我会使用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 database(我使用的是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
...

答案 1 :(得分:5)

您可以使用ROW_NUMBER()函数来模拟此行为:

SELECT t.*
FROM   (SELECT t.*, ROW_NUMBER() OVER (PARTITIN BY age ORDER BY 1) as rk
        FROM   some_table
) t
WHERE rk <= 3;