If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > get top 5 from sorted list

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-11, 11:34
capriman capriman is offline
Registered User
 
Join Date: Dec 2011
Posts: 6
get top 5 from sorted list

SELECT b.ISBN,sum(o.quantity) as TOTALQTYSold,avg((RETAIL-COST)/COST*100) AS PERcentPROFIT
FROM BOOKS b, orderitems o
WHERE b.isbn=o.isbn
group by b.isbn
order by TOTALqtysold desc;

here is my sql but need to only show the Top 5 Isbn and the rest of the columns for the top 5 quantity sold books????

thanks,
capriman
Reply With Quote
  #2 (permalink)  
Old 12-10-11, 12:27
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
>the rest of the columns for the top 5 quantity sold books????
How do we know what are the "rest of the columns" which should be returned?
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.

Last edited by anacedent; 12-10-11 at 12:33.
Reply With Quote
  #3 (permalink)  
Old 12-10-11, 12:53
The_Duck The_Duck is offline
Registered User
 
Join Date: Jul 2003
Posts: 2,292
read up on how to use analytic functions.
__________________
- The_Duck
you can lead someone to something but they will never learn anything ...
Reply With Quote
  #4 (permalink)  
Old 12-10-11, 13:46
capriman capriman is offline
Registered User
 
Join Date: Dec 2011
Posts: 6
just what is showing in the select

Just want to show the - isbn, total quantity, and the percentage of profit for the Top 5 highest total quantity. I looked in to the analytic func and not seeing how to do it?

thanks,
capriman
Reply With Quote
  #5 (permalink)  
Old 12-10-11, 14:24
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #6 (permalink)  
Old 12-10-11, 17:30
capriman capriman is offline
Registered User
 
Join Date: Dec 2011
Posts: 6
still not getting answer for top 5

SELECT b.ISBN,sum(o.quantity) as TOTALQTYSold,avg((RETAIL-COST)/COST*100) AS PERcentPROFIT
FROM BOOKS b, orderitems o
WHERE b.isbn=o.isbn and rownum < 6
group by b.isbn
order by TOTALqtysold desC;

getting

only two rows selected not clear on how to modify above

ISBN TOTALQTYSOLD PERCENTPROFIT
---------- ---------------------- ----------------------
0401140733 5 54.92957746478873239436619718309859154925
1059831198 1 65.12

2 rows selected


thanks for helping
capriman
Reply With Quote
  #7 (permalink)  
Old 12-10-11, 17:44
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
why not use analytics?

rownum for/from which table?
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #8 (permalink)  
Old 12-11-11, 00:19
flyboy flyboy is offline
Registered User
 
Join Date: Mar 2007
Posts: 546
Did you read the article anacedent posted the link? If so, here is another one: ORACLE-BASE - Top-N Queries
Congratulations for implementing the "What not to do!" method. I think its title is self-explanatory, but there are other correct methods described below that.

Exact query depends on the definition what "top 5 quantity" exactly means in case when some quantities are same. Shall it be only 5 rows even when there are 6 ISBNs with the same (greatest) quantity? Or do you want rows with 5 different (top) quantities?
Anyway, both cases are described in the article from the link above.

ISBN is unique in real world. If it is unique even in your data model (declared by unique or primary key constraint), just add "the rest of the columns" (only from BOOKS table) to the SELECT clause. In older Oracle versions, you will have to add them to GROUP BY clause, but, as they do not multiply resultset, that would not matter anyway.

Last edited by flyboy; 12-11-11 at 00:20. Reason: typos
Reply With Quote
  #9 (permalink)  
Old 12-11-11, 10:34
capriman capriman is offline
Registered User
 
Join Date: Dec 2011
Posts: 6
not sure where to insert the select clause to handle rownum

SELECT b.ISBN,sum(o.quantity) as TOTALQTYSold,avg((RETAIL-COST)/COST*100) AS PERcentPROFIT
FROM BOOKS b, orderitems o
WHERE b.isbn=o.isbn
group by b.isbn
order by TOTALqtysold desC;

the above works- but need that top 5
I created the rownum_order_test table as the article recommends but not sure where to nest the below SELECT STATEMENT TO get the top five???

SELECT val
FROM (SELECT val
FROM rownum_order_test
ORDER BY val DESC)
WHERE ROWNUM <= 5;

thanks soo much again,
capriman
Reply With Quote
  #10 (permalink)  
Old 12-11-11, 11:03
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
Code:
WITH nested
     AS (SELECT b.isbn,
                SUM(o.quantity)                     AS totalqtysold,
                Avg(( retail - cost ) / cost * 100) AS percentprofit
         FROM   books b,
                orderitems o
         WHERE  b.isbn = o.isbn
         GROUP  BY b.isbn
         ORDER  BY totalqtysold DESC)
SELECT *
FROM   nested
WHERE  ROWNUM < 6;
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #11 (permalink)  
Old 12-11-11, 14:23
capriman capriman is offline
Registered User
 
Join Date: Dec 2011
Posts: 6
Thanks that worked

Thanks soo much - that makes sense - never used the Nested word??

Thanks again,
Capriman
Reply With Quote
  #12 (permalink)  
Old 12-11-11, 14:45
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
when all else fails Read The Fine Manual

SELECT
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #13 (permalink)  
Old 12-11-11, 14:49
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
"Nested" could have been "test", "abc", "xyz", whatever - it is just a subquery or inline view name. Subquery factoring is also known as a "WITH clause"; quite a useful feature.
Reply With Quote
  #14 (permalink)  
Old 12-12-11, 10:47
beilstwh beilstwh is offline
Lead Application Develope
 
Join Date: Jun 2004
Location: Liverpool, NY USA
Posts: 2,222
Anacedent's with clause can be rewritten as

Code:
SELECT *
FROM   (SELECT b.isbn,
                SUM(o.quantity)                     AS totalqtysold,
                Avg(( retail - cost ) / cost * 100) AS percentprofit
         FROM   books b,
                orderitems o
         WHERE  b.isbn = o.isbn
         GROUP  BY b.isbn
         ORDER  BY totalqtysold DESC)
WHERE  ROWNUM < 6;
__________________
Bill
You do not need a parachute to skydive. You only need a parachute to skydive twice.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On