Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Capture nth Row

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2007-10-25 : 15:58:44
I need to run a query that will output every 25th row in a table. For example, if source table A has 500 rows, the result should show 20 rows.

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-10-25 : 16:40:14
Get the result into a temp table or table variable which has an identity column and do a select from the table where mod/25=0.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-26 : 02:59:03
quote:
Originally posted by dinakar

Get the result into a temp table or table variable which has an identity column and do a select from the table where mod/25=0. id_col%25=0

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-10-26 : 03:01:34
or use row_number()


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-26 : 03:07:55
quote:
Originally posted by qman

I need to run a query that will output every 25th row in a table. For example, if source table A has 500 rows, the result should show 20 rows.


Forget to note that you posted in sql server 2005 forum

Select columns from
(
select row_number() over (order by col) as n, columns from table
) as t
where n%25=0

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -