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 > Data Access, Manipulation & Batch Languages > ANSI SQL > 3 largest value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-04-03, 10:02
QaAP QaAP is offline
Registered User
 
Join Date: Jan 2003
Posts: 4
3 largest value

I can use MAX to find the largest value but Is there a way or function to find 3 largest value from a column?
Reply With Quote
  #2 (permalink)  
Old 03-04-03, 10:18
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: 3 largest value

Quote:
Originally posted by QaAP
I can use MAX to find the largest value but Is there a way or function to find 3 largest value from a column?
There are various ways. One is:
SELECT col FROM tab
WHERE 3 >
(SELECT COUNT(DISTINCT col) FROM tab tab2
WHERE tab2.col > tab.col);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 03-04-03, 11:19
QaAP QaAP is offline
Registered User
 
Join Date: Jan 2003
Posts: 4
Re: 3 largest value

Quote:
Originally posted by andrewst
There are various ways. One is:
SELECT col FROM tab
WHERE 3 >
(SELECT COUNT(DISTINCT col) FROM tab tab2
WHERE tab2.col > tab.col);
I need to get 3 largest values from 1 table and 1 column. eg.

Table1:
Name Age
abc 3
DAJ 8
YYY 19
TTT 25

3 largest column

Name AGE
-------------
DAJ 8
YYY 19
TTT 25
Reply With Quote
  #4 (permalink)  
Old 03-04-03, 11:23
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: 3 largest value

OK, so just change the columns in my previous answer:

SELECT name, age
FROM table1
WHERE 3 >
( SELECT COUNT(DISTINCT age)
FROM table1 t1
WHERE t1.age > table1.age
);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 10-05-06, 07:37
fondofopensource fondofopensource is offline
Registered User
 
Join Date: Oct 2006
Posts: 3
Red face printing 3rd largest number not working

dear friend,

The printing 3rd largest number not working in MySQL5.
************************************************** ******************************
select * from fun;
+-------+
| money |
+-------+
| 123 |
| 111 |
| 12 |
| 134 |
| 777 |
| 888 |
| 666 |
| 555 |
| 99999 |
| 256 |
| 777 |
+-------+
11 rows in set (0.00 sec)

************************************************** **********************************************


select f1.money from fun as f1 where 3>(select count(distinct f1.money) from fun as f1, fun as f2 where f1.money>f2.money order by f1.money desc);

Result is : Empty set (0.01 sec)



Please reply what is the solutin
Reply With Quote
  #6 (permalink)  
Old 10-05-06, 07:42
fondofopensource fondofopensource is offline
Registered User
 
Join Date: Oct 2006
Posts: 3
Red face to print 3rd largest number from a column in a table (MySql 5.0)

I want to print the 3rd largest number of a column say money from table fun as given.

printing 3rd largest number not working
Pls help

The printing 3rd largest number not working in MySQL5.
************************************************** ******************************
select * from fun;
+-------+
| money |
+-------+
| 123 |
| 111 |
| 12 |
| 134 |
| 777 |
| 888 |
| 666 |
| 555 |
| 99999 |
| 256 |
| 777 |
+-------+
11 rows in set (0.00 sec)

************************************************** **********************************************


select f1.money from fun as f1 where 3>(select count(distinct f1.money) from fun as f1, fun as f2 where f1.money>f2.money order by f1.money desc);

Result is : Empty set (0.01 sec)



Please reply what is the solution
Reply With Quote
  #7 (permalink)  
Old 10-05-06, 08:02
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
This smells like homework to me, but I'll give you a small hand by relocating your question to the MySQL forum. There is a MySQL-specific extension to SQL specifically to help with tasks like this.

-PatP
Reply With Quote
  #8 (permalink)  
Old 10-05-06, 08:09
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Same as I posted before really:

Code:
select f1.money 
from fun as f1 
where 3 >
( select count(distinct f2.money) 
  from fun as f2 
  where f1.money>f2.money
);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #9 (permalink)  
Old 10-05-06, 08:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
please don't post the same question into more than one forum

threads merged
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-05-06, 09:12
fondofopensource fondofopensource is offline
Registered User
 
Join Date: Oct 2006
Posts: 3
hi,

It is giving the result as:
mysql> select f1.money
-> from fun as f1
-> where 3 >
-> ( select count(distinct f2.money)
-> from fun as f2
-> where f1.money>f2.money
-> );
+-------+
| money |
+-------+
| 123 |
| 111 |
| 12 |
+-------+
3 rows in set (0.00 sec)

(i.e.) It is taking first three rows of the column money. But I want to print 3 greatest number. i.e.
99999
888
777
Reply With Quote
  #11 (permalink)  
Old 10-05-06, 09:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
somehow, the inequality was incorrectly reversed

this works (i tested it on your sample data) --
Code:
select distinct money 
from fun as f1 
where 3 >
( select count(distinct money) 
  from fun 
  where f1.money < money
)
order by money desc
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 10-05-06, 10:36
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Other solution, without subquery but with (self) join:
Code:
SELECT A.age, A.whatever
FROM   table1 AS A
        INNER JOIN table1 AS B
        ON A.age <= B.age
GROUP BY A.age, A.whatever  -- make sure this is at least a PK of table1
HAVING count(*) <= 3
If you just want to see the third largest, replace "<= 3" by "= 3".

Depending on the size of your table (and presence of indices etc.) the subquery solution or the join solution might be the most performant one.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #13 (permalink)  
Old 10-05-06, 10:38
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Yet another solution, but unfortunately a platform-specific one (just like the mysql one), now for DB2:
Code:
SELECT *
FROM   table1
ORDER BY age DESC
FETCH FIRST 3 ROWS ONLY
B.T.W., this is the most performant solution of the three when using DB2.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #14 (permalink)  
Old 10-05-06, 10:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
excuse me? which mysql-specific solution would you be referring to?

pat did not move the thread, we are still in the generic SQL forum, and all the solutions offered up until your last have been good, standard sql

granted, there is a cleaner mysql solution, but fondofopensource re-opened a three-year-old thread in the SQL forum as well as starting a duplicate post in the mysql forum, so when i merged the threads, i left the merged thread here

let's wait to see if he/she is interested in the mysql solution too...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 10-05-06, 11:01
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Sorry, then.
You are right.
Actually, I didn't pay much attention to the mysql stuff being said
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
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