Kevin Nelson Marshall
Other entries:
« I don't know if it's a good thing or a bad thing...

Every so often I jump into Google analytics and try to figure out just who is hitting this blog and what they are reading...today was one of those random days.

In looking at the logs, I noticed that someone stumbled here via Google while attempting to find a solution on how to get every other row in a table with T-SQL...and though they are probably already long gone, I thought I would give my quick answer anyway (who knows maybe someone else will have the same question down the road?)...

Basically, any time you are thinking about an every other situation (or any skip X type of thing for that matter), you should be thinking about a modulo ... and in T-SQL you use the % symbol to do modulo.

So here is the quick answer:

select * from (select *, (account_id % 2) mod from account) tmp where mod = 0



What you are basically doing is dividing the account_id of each record by 2...if there is a remainder, then it's an odd number...if there is not a remainder, then it's an even number...in this example, we want all the records with even numbers so we check that the remainder is zero (0).

Now technically this is not truly 'every other' record...because it's based on your account_id and assumes that you are not missing any account_id in your sequence of records (for example you could have deleted account_id = 2 and so now there is no gap between 1 and 3 and yet neither 1 or 3 would be pulled up by this query)...

But on a generic level it will get you what you want.

If you really want to get fancy you can combine the count() function (with a group by) to dynamically generate the number that you then apply a modulo to...and that should work for true 'every other'...but I'll leave that as something for you to try and implement/play with...for some ideas on getting started with this approach check out http://support.microsoft.com/default.aspx?scid=KB;EN-US;q186133

Also I feel like I should at least note the fact that it's probably VERY rare that you would need to do something like this 'every other row' stuff...

It's much more likely that you should just write a better where clause (perhaps spend some more time thinking about what data you are really trying to get at), or maybe a random chunk of rows...but that's just my two cents. And it's nice to know that if you do find a reason to get at every other row...now you know you can!

posted by Kevin Marshall on 2008-06-25 00:00:00+00

Subscribe »

BotFu feed with RSS reader

BotFu feed by Email


Search All Posts »

Blog Details »

This blog now includes 286 wonderfully exciting posts from 1 unique and very special writer!


Kevin Marshall - Who's That?

I'm just your basic programmer. I can't spell to save my life, I'm not the greatest story teller, and I often ramble on about nothing. This blog showcases all of that!

If you're bored drop me an email at info at falicon.com or view my outdated resume.


Stalk me on »

Twitter (@falicon) »
Delicious »
Digg »
Disqus »
Facebook »
Flickr »
FriendFeed »
Last.fm »
LinkedIn »
StumbleUpon »

Archives by Category »

(24) Code »
(5) ColdFusion »
(11) Database »
(7) Factor »
(286) General »
(9) JavaScript »
(15) Perl »
(13) PHP »
(17) Ruby »

Archives by Month »

(1) February 2010 »
(5) January 2010 »
(2) October 2009 »
(6) August 2009 »
(11) July 2009 »
(2) May 2009 »
(3) April 2009 »
(2) March 2009 »
(7) February 2009 »
(9) January 2009 »
(14) December 2008 »
(5) November 2008 »
(12) October 2008 »
(13) September 2008 »
(16) August 2008 »
(23) July 2008 »
(20) June 2008 »
(24) May 2008 »
(23) April 2008 »
(27) March 2008 »
(28) February 2008 »
(26) January 2008 »
(7) December 2007 »

Published Works »

Beginning Amazon's SimpleDB (Apress in dev.)
Pro Active Record (Apress 2007)
Web Services with Rails (O'Reilly 2006).

Contributed To »

Ruby Cookbook (O'Reilly 2006)
SQL Cookbook (O'Reilly 2005)
Various Reviews published in Computing Reviews

Free Code I've Created »

SimpleDB library in Python 3.0

Fantasy focused domains »

draftwizard.com
fantasy-football-draft.com
fantasyfootballkit.com
fantasyfootballquiz.com
hockeynotes.com
pegg.it
rosterhelp.com
sportsxml.com
statsfeed.com
supermug.com

Tech. focused domains »

factorcode.com
perlquiz.com
simpledb.info

Social Tool focused domains »

conversationlist.com
friendstat.us
fuzzypop.com
gawk.it
grou.pe
halfbite.com
jivegas.com
pu.ly
tagli.st
timelylinks.com
tym.ly
wow.ly

Utility focused domains »

fubnub.com

Other domains »

betaread.com
botfu.com
falicon.com
storyrank.com

Not yet live domains »

bar.ackoba.ma
basketballnotes.com
buddydirt.com
budrank.com
cakntoba.com
coachwizard.com
cointhief.com
ezbcs.com
falconsrule.com
fantasydeke.com
fantasyfootballrank.com
ffkit.com
footballnotes.com
footballpublishing.com
giggletweet.com
greentile.com
herobrawl.com
kacode.com
kickasscode.com
knowabout.it
leaguewizard.com
nfldraftnews.com
pa.ly
rorbe.com
slidepitch.com
startfail.com
survivorhub.com
tagli.st
thedfl.com
thescoutsreport.com
toptenify.com
tripacation.com
tweetwiki.com
umock.com

* Yes I realize I have a bit of an addiction to domain names, but I really do have specific ideas for each of the above.



This blog is powered by KickAssCode.