Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Avoid common pitfalls of incorporating spreadsheets into Java apps. Read about it in the free white paper: “Five Biggest Blunders when Building Spreadsheet Applications in JavaDownload Now!
  #1  
Old August 8th, 2004, 04:23 PM
Rhynna Rhynna is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 Rhynna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nested aggregate query help needed

I am having problems with a nested aggregate function. What it need is to find the upc ordered most often and then return its manufacturer name.

The tables are:

ORDERITEMS
ORDER#, ITEM#, UPC, QUANTITY

UPCMANU
UPC, MANID

MANUFACTURER
MANUID
NAME

There is an ORDERS table that holds the customer order info, but I don't believe it is needed for this query.

I can query to find the total times every upc has been ordered, but I am at a loss at how to find and return the most often ordered upc to an outer query to find the manuid.

select upc, sum(quantity)
from orderitems
group by upc;

Any help would be much appreciated!

Reply With Quote
  #2  
Old August 9th, 2004, 10:44 AM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 31 m 56 sec
Reputation Power: 8
To determine the number of orders per UPC you can use
SELECT upc, count(upc)
from orderitems
group upc
order by upc desc;
...
You can place this data into a temporary table then query the max value for this.
...
Or you could join the orderitems table to the manufacturer table to learn the name in the same query.
SELECT m.name, o.upc, count(o.upc)
from orderitems o, manufacturer m, upcmanu u
where o.upc = u.upc
and u.manid = m.manuid
group upc
order by upc desc;
...
__________________
El éxito consiste en una serie de pequeñas victorias día a día

MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html

Reply With Quote
  #3  
Old August 9th, 2004, 11:56 AM
KK2796 KK2796 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 11 KK2796 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 40 sec
Reputation Power: 0
My solution has three parts.

Part1 (the innermost query), adds up all of the totals for each UPC, then orders them by totals (with the record with the highest total first).

Part 2 pulls the very first row from part1 - this will be the UPC with the highest total count (if you want the top 5 UPC's, just change "ROWNUM < 2" to "ROWNUM < 6").

Part 3 joins the results of part 2 to the munfacturing tables to get the UPC's details.

Code:
SELECT MANUFACTURER.NAME, UPCMANU.UPC, UPC_TOTAL 
FROM UPCMANU, MANUFACTURER,
    (
    SELECT UPC, UPC_TOTAL FROM
        (
        SELECT UPC, SUM(QUANTITY) UPC_TOTAL 
        FROM ORDERITEMS 
        GROUP BY UPC
        ORDER BY 2 DESC
        ) PART1
    WHERE ROWNUM < 2
    ) PART2
WHERE UPCMANU.UPC = PART2.UPC
  AND  MANUFACTURER.MANID = UPCMANU.MANID

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Nested aggregate query help needed


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

VeriSign Code Signing




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway