April 9, 2009 at 9:22 am
Guys - this is simple but i been struggling to figure out in 2000 where you cant use MOD function.
I want to display/select every 13th row from a table with 20K rows in it, so basically it should pull every 13th, 26, 39, 52, 65....
Please help and my table is in SQL Server 2000. So pls show solution which is compatable with 2000.
Many Thanks
Arun
April 9, 2009 at 9:28 am
Do you have an order for them, or just pick which one is record 13 at random?
You could insert into a temp table, with an Identity column, and select the rows where ID%13 = 0. That might work.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
April 9, 2009 at 9:30 am
Yes sorry I forgot to mention I have a UNIQUEID Column which starts from 1 and I need to pick 13th row based on this Unique ID.
Whats the best and quick solution?
Thanks for responding so quick.
April 9, 2009 at 10:14 am
arun_anand09 (4/9/2009)
Yes sorry I forgot to mention I have a UNIQUEID Column which starts from 1 and I need to pick 13th row based on this Unique ID.Whats the best and quick solution?
Thanks for responding so quick.
SELECT TOP 1 * FROM (
SELECT TOP 13 * FROM mytable ORDER BY UNIQUEID
) d ORDER BY UNIQUEID DESC
EDIT:apologies, read the spec too quick.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 9, 2009 at 12:18 pm
i think th etrick is to simply use integer division to get the mod = 0
here's how to do it with a tally table:
select N from master.dbo.Tally
where n = 1 or n % 13 = 0
and a guestimmate based on what you said about your table structure
SELECT * FROM mytable
where UNIQUEID = 1 or UNIQUEID % 13 = 0
ORDER BY UNIQUEID
Lowell
April 9, 2009 at 1:08 pm
That's great both the solution works.
This is a brilliant community - Great work Guys!
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply