Question: I have a table similar to the one below.

[![enter image description here](https://i.stack.imgur.com/Q4kEl.png)](https://i.stack.imgur.com/Q4kEl.png)

What I want to do is to select the rows with consecutive RowNo with the same job name must be selected if it begins with RowNo = 1. Here is the sample output:

[![enter image description here](https://i.stack.imgur.com/HoduU.png)](https://i.stack.imgur.com/HoduU.png)

Hope you can help. Thank you.

Answer: 
Try this

```
DECLARE @Tbl TABLE (RowNo INT, Jobname NVARCHAR(50), AuditDate DATETIME)
INSERT INTO @Tbl
SELECT 3, 'Backup Database Sales', '2016.07.26' UNION ALL
SELECT 1, 'Send Autoemail Sales Report', '2016.07.26' UNION ALL
SELECT 2, 'Send Autoemail Sales Report', '2016.07.25' UNION ALL
SELECT 3, 'Send Autoemail Sales Report', '2016.07.24' UNION ALL
SELECT 4, 'Update Sales Stats', '2016.07.23' UNION ALL
SELECT 4, 'Update Sales Stats', '2016.07.22' UNION ALL
SELECT 1, 'Generate new item codes', '2016.07.26' UNION ALL
SELECT 2, 'Generate new item codes', '2016.07.25' 

;WITH CTE
AS
(
    SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Id FROM @Tbl    
)

SELECT
    *
FROM
    @Tbl T
WHERE
    EXISTS
    (
        SELECT TOP 1
            1
        FROM
        (
            SELECT 
                C.Jobname,
                MIN(C.RowNo) MinRowNo,
                MAX(C.RowNo) MaxRow
            FROM 
                CTE C
            GROUP BY
                C.Jobname,
                C.Id - C.RowNo
         ) A
         WHERE
            A.MinRowNo <> A.MaxRow AND
            A.MinRowNo = 1 AND
            A.Jobname = T.Jobname AND
            T.RowNo BETWEEN A.MinRowNo AND A.MaxRow
    )

```

Output

```
RowNo       Jobname                                            AuditDate
----------- -------------------------------------------------- -----------------------
1           Send Autoemail Sales Report                        2016-07-26 00:00:00.000
2           Send Autoemail Sales Report                        2016-07-25 00:00:00.000
3           Send Autoemail Sales Report                        2016-07-24 00:00:00.000
1           Generate new item codes                            2016-07-26 00:00:00.000
2           Generate new item codes                            2016-07-25 00:00:00.000

```
```
SELECT T1.* 
       FROM 
            YourTable T1 
       INNER Join 
             YourTable T2 
                  ON T1.RowNo = 1 AND T2.RowNo =1 AND T1.JobName=T2.Jobname
                       OR T1.RowNo > 1 AND T1.RowNo - 1 = T2.RowNo AND T1.JobName=T2.Jobname

```