Vladimir
Vladimir

Reputation: 351

select up to N rows for each unique value of the column

I have a table with next columns:

First Name,
Last Name,
Age

Lets assume there we have

I want get recordset where I will have up to N records for each age. (records could be random)

Could you advice?

For example if N = 3 then we will have

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

Upvotes: 1

Views: 1595

Answers (2)

Bogdan Sahlean
Bogdan Sahlean

Reputation: 1

I would use ROW_NUMBER function thus:

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

If you have AdventureWorks database installed (I used AdventureWorks2008) then you could use this script for testing:

-- 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

Results:

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
...

Upvotes: 5

Mureinik
Mureinik

Reputation: 311188

You can use the ROW_NUMBER() function to simulate this behavior:

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

Upvotes: 5

Related Questions