Given the following table structure
| Column |
|---|
| Id |
| Name |
| DateCreated |
with the following data
| id | Name | DateCreated |
|---|---|---|
| 1 | Joe | 1/13/2021 |
| 2 | Fred | 1/13/2021 |
| 3 | Bob | 1/12/2021 |
| 4 | Sue | 1/12/2021 |
| 5 | Sally | 1/10/2021 |
| 6 | Alex | 1/9/2021 |
I need SQL that will page over the data based on datecreated. The query should return the top 3 records, and any record which also shares the datecreated of the top 3.
So give the data above, we should get back Joe, Fred and Bob (as the top 3 records) plus Sue since sue has the same date as Bob.
Is there something like ROW_NUMBER that increments for each row where it encounters a different value.
For some context this query is being used to generate an agenda type view, and once we select any date we want to keep all data for that date together.
EDIT I do have a solution but it smells:
;WITH CTE AS ( SELECT ROW_NUMBER() OVER(ORDER BY DateCreated DESC) RowNum,CAST(DateCreated AS DATE) DateCreated,Name
FROM MyTable),
PAGE AS (SELECT *
FROM CTE
WHERE RowNum<=5)
SELECT *
FROM Page
UNION
SELECT *
FROM CTE
WHERE DateCreated=(SELECT MIN(DateCreated) FROM Page)


