Home > Ask the Oracle Database / Applications Experts > SQL Questions & Answers > Using a derived table instead of a view
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Using a derived table instead of a view

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 got some results from two tables (products, order_details) using a group by clause on prod_name:

prod_name   sum(quantity)
 apple       100
 Mango       200
 orange      400 

But what I want is the max of these sums (quantity), i.e orange 400. Can I get it in a single query statement? (I don't want to create a view and select the max value from it.)


>

Yes, you can do this with a derived table.

select prod_name
     , sumquantity  as maxsumquantity
  from (
       select prod_name
            , sum(quantity) as sumquantity
         from products
       inner
         join order_details
           on products.id
            = order_details.prod_id
       group
           by prod_name
       ) as dt 
 where sumquantity
     = (
       select max(sumquantity)
         from (
              select prod_name
                   , sum(quantity) as sumquantity
                from products
              inner
                join order_details
                  on products.id
                   = order_details.prod_id
              group
                  by prod_name
              ) as dt     
       ) 

Notice that you have to write the derived table twice. If you were to define a view, then the query would be:

select prod_name
     , sumquantity  as maxsumquantity
  from dtview
 where sumquantity
     = (
       select max(sumquantity)
         from dtview
       ) 

Maybe it's me, but I'd rather type the subquery which defines the derived table only once (to define a view), than type it every place it's needed.

On the other hand, there is often a simpler way to get what you want, depending on the specific requirements. In this situation, if you're using Microsoft SQL Server or Access, you can also get the answer you want like this:

select top 1
       prod_name
     , sum(quantity) as sumquantity
  from products
inner
  join order_details
    on products.id
     = order_details.prod_id
group
    by prod_name
order    
    by sumquantity desc

In MySQL, you'd use LIMIT instead of TOP.

For More Information


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



RELATED CONTENT
SQL
How to check SQL query construction with the Mimer Validator
Using the SQL GROUP BY clause for counting combinations
How to use an SQL CASE expression
How to sort an SQL UNION query with special ORDER BY sequence
How to use string functions to make an SQL join
An SQL solution for a customer order homework problem
How to use SQL's POSITION function with substrings
Using SQL date functions to get totals for last three days
Using CASE in the SQL ORDER BY clause
What's the difference between an SQL inner join and equijoin?

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 technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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