Home > Ask the Oracle Experts > SQL Questions & Answers > Select only the second of duplicates
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Select only the second of duplicates

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: 21 March 2003

All I want to do is print the second tuple where the NO is repeated:

 NO  NAME
 1   Name1
 2   Name2
 2   Name3
 2   Name4
 3   Name5
 3   Name6
 4   Name7
 5   Name8
 5   Name9

When I run my SQL query, my output must be --

 NO  NAME
 2   Name3
 3   Name6
 5   Name9

What is the query?


>
EXPERT RESPONSE

The first thing we need to establish is what you mean by the "second" tuple. There must be a sequence, a way of ordering the rows, that allows us to determine which one is first, and which one is second. By your example, I shall assume that "first" means the lowest collating NAME.

Now for the problem of finding the "second" one. There may be other ways to do it, but here's how I would approach this problem.

The "first" one is the lowest name, and we can get this with the MIN() function and grouping on NO:

select NO, min(NAME)
  from yourtable
group
    by NO

The "second" one is trickier. It is the lowest one that isn't the first one. But this time, instead of using GROUP BY, we use another method to produce grouping, the correlated subquery.

select NO, NAME
  from yourtable XX
 where NAME =
       ( select min(NAME)
           from yourtable
          where NO = XX.NO
            and NAME >
                ( select min(NAME)
                    from yourtable
                   where NO = XX.NO
                )
       )

To see how this works, consider any row in the outer query. Using the correlation variable XX to refer to this row in the outer query, the innermost subquery gets the lowest name for all rows with the same value of NO as the XX row being considered. This lowest value is used by the next outer subquery, which gets the lowest name that's greater than the lowest which was found by the innermost subquery. In other words, the second lowest. Then the outer query gets the row that has that second lowest name.

It's easy when you see it explained, but kind of hard to come up with on your own if you've never seen it before.

For More Information


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