Home > Ask the Oracle Experts > SQL Questions & Answers > Groups having only one value in a column
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Groups having only one value in a column

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: 12 July 2004

I have a table Invent with the following sample data.

nSeqID  Name  Status
 1      ABC      y
 1      BCD      y
 1      CEF      y
 2      GHI      y
 2      HIJ      n
 2      IJK      y
 3      JKL      y
 3      KLM      y
 3      LMN      y

I need to show the result then group the results based on nSeqID and which the status should be 'y'. Required output is as follows:

nSeqID  Name  Status
 1      ABC      y
 1      BCD      y
 1      CEF      y
 3      JKL      y
 3      KLM      y
 3      LMN      y

>
EXPERT RESPONSE

Every once in a while, a question is submitted to us here at Ask The Expert which looks simple on the surface, but turns out to be tricky. My first reaction to this question was to use a simple WHERE clause:

select nSeqID  
     , Name  
     , Status
  from Invent
 where Status = 'y'

However, this is wrong, because while it eliminates the nSeqID=2 row where Status='n', it will not eliminate the other rows for nSeqID=2.

The solution involves, as you suggested, grouping. We need to form groups of rows, one group per nSeqID, and make sure that the only Status within each group is 'y'. This is done by counting the number of different statuses, and then making sure that there's only one of them, and that its lowest value is 'y'. This may sound weird, but it works quite well, and there's no easier way to say it.

select nSeqID  
     , Name  
     , Status
  from Invent
 where nSeqID
    in (
       select nSeqID
         from Invent
       group
           by nSeqID
       having count(distinct Status) = 1
          and min(Status) = 'y'
       ) 

The subquery decides which nSeqIDs have only Status='y' rows. Then the outer query returns all rows for those nSeqIDs.

Notice that you can't put the Status='y' condition into a WHERE clause in the subquery, because that would return all nSeqIDs which have a Status='y', even if they have a Status='n' too, because the Status='n' rows wouldn't make it to the grouping step in the subquery. Also, note that we must use an aggregate function in the HAVING clause because syntax rules demand it, but we could use either MIN or MAX, because both of them will return the same result, since there's only one value in the group.

For More Information


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


RELATED CONTENT
SQL
Using the SQL date function to find aggregate totals by month
Using an SQL SELECT statement from a non-existing table
Using LEFT OUTER JOIN query to get zero row counts in SQL
How to return multiple values for THEN clause in an SQL CASE expression
Can I concatenate row values in SQL?
Should I try to avoid a LEFT OUTER JOIN in SQL?
Tips for derived tables in SQL and using FULL OUTER JOINs
How to write an SQL query for two foreign keys to the same table
How to create an SQL CHECK constraint for two letters
How to return a zero in SQL instead of no row back for a select count

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



Oracle White Papers: Fusion Middleware
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