Selecting specific rows SQL

All we need is an easy explanation of the problem, so here it is.

I want a query to select 2 rows for every NumberId from the table that occur without being interrupted by another NumberId, and same for another NumberId and so on

ID      Name         NumberId
---------------------------
1       Abc          1
2       bcd          1
3       cde          1
4       def          2
5       efg          2
6       fgh          2
7       ghij         2 
8       defg         1
9       efgj         1
10      fghi         1
11      ghij         1 

I want to select 2 rows from NumberId 1 and 2 rows from NumberId 2 and again 2 rows from NumberId 1

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Try this.

--set up demo data
declare @T table (id int, name varchar(10), NumberId int)
insert into @T(id, name, NumberId) values
(1,'Abc',1),
(2,'bcd',1),
(3,'cde',1),
(4,'def',2),
(5,'efg',2),
(6,'fgh',2),
(7,'ghij',2), 
(8,'defg',1),
(9,'efgj',1),
(10,'fghi',1),
(11,'ghij',1 )
;

I used a common table expression to create groupings of consecutive NumberId’s

–Create a grouping for consecutive values called grp

;WITH _cte
AS (
    SELECT *
        ,DATEADD(DAY, - ROW_NUMBER() OVER (
                PARTITION BY Numberid ORDER BY [id]
                ), [id]) AS grp
    FROM @T
    )
    select * from _cte

| id | name | NumberId | grp                     |
|----|------|----------|-------------------------|
| 1  | Abc  | 1        | 1900-01-01 00:00:00.000 |
| 2  | bcd  | 1        | 1900-01-01 00:00:00.000 |
| 3  | cde  | 1        | 1900-01-01 00:00:00.000 |
| 4  | def  | 2        | 1900-01-04 00:00:00.000 |
| 5  | efg  | 2        | 1900-01-04 00:00:00.000 |
| 6  | fgh  | 2        | 1900-01-04 00:00:00.000 |
| 7  | ghij | 2        | 1900-01-04 00:00:00.000 |
| 8  | defg | 1        | 1900-01-05 00:00:00.000 |
| 9  | efgj | 1        | 1900-01-05 00:00:00.000 |
| 10 | fghi | 1        | 1900-01-05 00:00:00.000 |
| 11 | ghij | 1        | 1900-01-05 00:00:00.000 |

Then, I used another common table expression to add a row_number to each row in the above result


;WITH _cte
AS (
    SELECT *
        ,DATEADD(DAY, - ROW_NUMBER() OVER (
                PARTITION BY Numberid ORDER BY [id]
                ), [id]) AS grp
    FROM @T
    )
    select * from _cte order by id
    ,AddedRn    --add a row number for each entry in the group
AS (
    SELECT *
        ,ROW_NUMBER() OVER (
            PARTITION BY grp ORDER BY id
            ) AS rn
    FROM _cte
    )

| id | name | NumberId | grp                     | rn |
|----|------|----------|-------------------------|----|
| 1  | Abc  | 1        | 1900-01-01 00:00:00.000 | 1  |
| 2  | bcd  | 1        | 1900-01-01 00:00:00.000 | 2  |
| 3  | cde  | 1        | 1900-01-01 00:00:00.000 | 3  |
| 4  | def  | 2        | 1900-01-04 00:00:00.000 | 1  |
| 5  | efg  | 2        | 1900-01-04 00:00:00.000 | 2  |
| 6  | fgh  | 2        | 1900-01-04 00:00:00.000 | 3  |
| 7  | ghij | 2        | 1900-01-04 00:00:00.000 | 4  |
| 8  | defg | 1        | 1900-01-05 00:00:00.000 | 1  |
| 9  | efgj | 1        | 1900-01-05 00:00:00.000 | 2  |
| 10 | fghi | 1        | 1900-01-05 00:00:00.000 | 3  |
| 11 | ghij | 1        | 1900-01-05 00:00:00.000 | 4  |

Now put it all together and only select rows where rn in (1,2)


--set up demo data
declare @T table (id int, name varchar(10), NumberId int)
insert into @T(id, name, NumberId) values
(1,'Abc',1),
(2,'bcd',1),
(3,'cde',1),
(4,'def',2),
(5,'efg',2),
(6,'fgh',2),
(7,'ghij',2), 
(8,'defg',1),
(9,'efgj',1),
(10,'fghi',1),
(11,'ghij',1 )
;

--Create a grouping for consecutive values called grp
WITH _cte
AS (
    SELECT *
        ,DATEADD(DAY, - ROW_NUMBER() OVER (
                PARTITION BY Numberid ORDER BY [id]
                ), [id]) AS grp
    FROM @T
    )
    ,AddedRn    --add a row number for each entry in the group
AS (
    SELECT *
        ,ROW_NUMBER() OVER (
            PARTITION BY grp ORDER BY id
            ) AS rn
    FROM _cte
    )
--Select only the rows with rn in (1,2)
SELECT *    
FROM AddedRn
WHERE rn IN (1,2)
ORDER BY id

| id | name | NumberId | grp                     | rn |
|----|------|----------|-------------------------|----|
| 1  | Abc  | 1        | 1900-01-01 00:00:00.000 | 1  |
| 2  | bcd  | 1        | 1900-01-01 00:00:00.000 | 2  |
| 4  | def  | 2        | 1900-01-04 00:00:00.000 | 1  |
| 5  | efg  | 2        | 1900-01-04 00:00:00.000 | 2  |
| 8  | defg | 1        | 1900-01-05 00:00:00.000 | 1  |
| 9  | efgj | 1        | 1900-01-05 00:00:00.000 | 2  |

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply