Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server, select a record every 'x' amount of records

enter image description here

Hi,

I have “Table1” as shown above with some records on it and the column names are Id, Name and [Entry Date]. Column Id is primary key, I am using SQL Server 2005.

I want to write a query that returns information every 5 records, for example my query will return:

enter image description here

As you can see in the final result only the record every 5 records was selected, how can I accomplish this?

Thank you

like image 400
Fayde Avatar asked Jan 29 '12 17:01

Fayde


1 Answers

SELECT id, name, entryDate 
  FROM (SELECT ROW_NUMBER() OVER(ORDER BY id) rowNumber, Id, name, entryDate
          FROM MyTable)
  WHERE rowNumber = 1
     OR rowNumber % 5 = 0
like image 173
Bassam Mehanni Avatar answered Sep 18 '22 13:09

Bassam Mehanni