Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: query for top customer

RE: query for top customer

From: Thomas, Kevin <Kevin.Thomas_at_calanais.com>
Date: Mon, 10 Sep 2001 08:19:28 -0700
Message-ID: <F001.00388254.20010910073519@fatcity.com>

Try this...

DECLARE
  CURSOR c_sales IS
  SELECT *
    FROM sales
   ORDER BY sales desc;
  r_sales c_sales%rowtype;
  l_count NUMBER := 0;
BEGIN
  OPEN c_sales;
  DBMS_OUTPUT.PUT_LINE( 'Customer' || ' ' || 'Sales' );   WHILE l_count < 3 LOOP
    FETCH c_sales INTO r_sales;
    DBMS_OUTPUT.PUT_LINE( r_sales.customer || ' ' || r_sales.sales );     l_count := l_count + 1;
  END LOOP;
  CLOSE c_sales;
END;
/

I did it as a pl/sql block because you can't be fancy (well not in 7.3.4) and use ROWNUM with an ORDER BY. I was going to suggest just doing:

SELECT *
  FROM sales
 WHERE rownum < 4
 ORDER BY sales DESC;

But alas, this doesn't work...try the above, it's nasty but quick...

HTH, Kev.


Kevin Thomas
Technical Analyst
Deregulation Services
Calanais Ltd.
(2nd Floor East - Weirs Building)
Tel: 0141 568 2377
Fax: 0141 568 2366
http://www.calanais.com

-----Original Message-----
Sent: 10 September 2001 14:30
To: Multiple recipients of list ORACLE-L

how to write a query to find top 3 customer based on their sales .
eg.
TABLE A

customer       sales
A100          100
A101           200
A102           105
A103            109
A104            108

RESULTS should be..
A101       200
A103        109
A104        108

Thanks in advance
Brajesh        

--

Please see the official ORACLE-L FAQ: http://www.orafaq.com
--

Author: Oracle DBA
  INET: oradba_at_tubes.tatasteel.com

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
--

Please see the official ORACLE-L FAQ: http://www.orafaq.com
--

Author: Thomas, Kevin
  INET: Kevin.Thomas_at_calanais.com
Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). Received on Mon Sep 10 2001 - 10:19:28 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US