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 2000 Forums
 Transact-SQL (2000)
 simple SELECT Query

Author  Topic 

hydtohou
Starting Member

24 Posts

Posted - 2006-11-18 : 14:39:12
I would like to select every N'th row from a table.
i.e rows with TABLEA.IDA values of 1,4,7 etc.

I know i can loop through , but is there any keyword which can do this.

Regards,

iminore
Posting Yak Master

141 Posts

Posted - 2006-11-18 : 14:55:18
Read about modulo in BOL
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2006-11-18 : 15:01:06
if you have a column with a rownumber value in it, you can use the mod operator to do this.

That is:

declare @t table (id int)
insert into @t
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9

select * from @t where (id + 2) % 3 = 0

if you don't have a rownumber column, see this article:
http://www.sqlteam.com/item.asp?ItemID=1491


SqlSpec - a fast, cheap, and comprehensive data dictionary generator for
SQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org
Go to Top of Page

hydtohou
Starting Member

24 Posts

Posted - 2006-11-18 : 15:47:59
Man be a general solution can be::

declare @num int
set @num = 3

select * from @t where (id + (@num-1)) % @num = 0


Thanks a lot..........


quote:
Originally posted by jezemine

if you have a column with a rownumber value in it, you can use the mod operator to do this.

That is:

declare @t table (id int)
insert into @t
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9

select * from @t where (id + 2) % 3 = 0

if you don't have a rownumber column, see this article:
http://www.sqlteam.com/item.asp?ItemID=1491


SqlSpec - a fast, cheap, and comprehensive data dictionary generator for
SQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-19 : 12:16:34
What if the ID's are not consecutive?
declare @t table (id int)
insert into @t
select 1 union all
select 23 union all
select 53 union all
select 342 union all
select 232 union all
select 5332 union all
select 13 union all
select -234 union all
select 0


select id, case when orderby % 3 = 0 then 'third' else '' end position from (
select t.*, (select count(*) from @t x where x.id < t.id) orderby
from @t t) q
order by orderby


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2006-11-19 : 13:00:07
yea, that works too.

I pointed the OP to graz's rownumber article if there's no rownumber column already. Not clear which has better perf, too lazy to test. My intuition is that graz's solution of creating a temp table with an idetity column is heavier on the disk, but probably less cpu intensive.

EDIT: just noticed that graz's article has Peso's sol'n at the end of it :)


SqlSpec - a fast, cheap, and comprehensive data dictionary generator for
SQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org
Go to Top of Page
   

- Advertisement -