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.
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 |
 |
|
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 allselect 2 union allselect 3 union allselect 4 union allselect 5 union allselect 6 union allselect 7 union allselect 8 union allselect 9select * from @t where (id + 2) % 3 = 0if 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 forSQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org |
 |
|
hydtohou
Starting Member
24 Posts |
Posted - 2006-11-18 : 15:47:59
|
Man be a general solution can be::declare @num intset @num = 3select * from @t where (id + (@num-1)) % @num = 0Thanks 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 allselect 2 union allselect 3 union allselect 4 union allselect 5 union allselect 6 union allselect 7 union allselect 8 union allselect 9select * from @t where (id + 2) % 3 = 0if 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 forSQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org
|
 |
|
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 allselect 23 union allselect 53 union allselect 342 union allselect 232 union allselect 5332 union allselect 13 union allselect -234 union allselect 0select id, case when orderby % 3 = 0 then 'third' else '' end position from (select t.*, (select count(*) from @t x where x.id < t.id) orderbyfrom @t t) qorder by orderby Peter LarssonHelsingborg, Sweden |
 |
|
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 forSQL Server 2000/2005 and Analysis Server 2005 - http://www.elsasoft.org |
 |
|
|
|
|
|
|