Home > Ask the Oracle Experts > SQL Questions & Answers > Selecting every 100th of 1,000,000 rows
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Selecting every 100th of 1,000,000 rows

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 20 September 2002
How can I select every 100th record for a table of 1,000,000 rows? For example, the table consists of a distinct memberid, sex, and age, and I only want every 100th member from this table.

>
EXPERT RESPONSE

There's really no perfect way to do this with just SQL. The best solution is to use an extension to SQL like Transact-SQL (Sybase and Microsoft SQL/Server) or PL/SQL (Oracle). These language extensions allow you to write processing logic to cursor through the table, fetching every 100th row using a loop counter. This is not only the most accurate method but the most efficient as well.

If your table's memberid is an auto_increment or identity or sequence column, and if it has relatively few gaps in the numeric sequence, then you can come close to what you want with the following query --

select memberid, sex, age
  from yourtable
 where mod(memberid+27,100) = 0

Here mod() is the modulus function, which returns a remainder upon division. Check your database for the specific function name. I added 27 to the memberid simply to introduce some randomness. The query will select memberids 73, 173, 273, and so on, if they exist. Out of a million rows, this should yield ten thousand, fewer if there are gaps.

You see, a lot depends on what you want these rows for. If you need to do a strict statistical calculation, and need exactly every 100th row, you would want the first solution. If you're just interested in getting approximately one out of every hundred rows, more or less, for testing purposes, then the modulus approach will usually suffice.

Finally, a recommendation I made in a previous answer, Selecting random rows (29 May 2001):

The best solution, for situations where you only need a small number of rows, such as in testing, is to have a sample table which contains not random rows of live data, but test cases -- in other words, a place where you can collect those rows which are of interest or which represent conditions you want to test for.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts