Selecting specific rows SQL 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
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  |