views:

30

answers:

1

how to select 1st, 4th, 7th, 10th... row from a table in SQL if i HAVING RECORDS LIKE BELOW

id  Name
1   a
2   b
3   c
4   b
5   s
6   h
7   k
8   g

i need to select 1 st, 4th, 7th, 10th rows

pls help me thanks Janarthanan M

+5  A: 

I don't have access to SQL Server right now, but I believe this should work. It works for PostgreSQL:

SELECT * FROM
(SELECT 
ROW_NUMBER () OVER (ORDER BY id) AS RowNumber, 
id, name FROM test) X
WHERE (RowNumber % 3) = 1
Thomas Mueller
yes, would work for SQL Server too
gbn