Advertisement

08.28.2006 at 01:58PM PDT, ID: 21970196
[x]
Attachment Details

Aggregate function that is equivalent of Top n?

Asked by Igiwwa in MySQL

Tags: top, n, aggregate, function

Given the following table and info:

ID    Value   Order
1       A         10
2       A          8
2       C         10
2       D          9
3       E          8
3       F         10
4       A          9
4       B         10
4       C          8
4       E          7

I want the top 2 Values in descending order for each ID.  In other words, the result set should look like this:

ID    Value   Order
1       A         10
2       C         10
2       D          9
3       F         10
3       E          8
4       B         10
4       A          9

So it would be the equivalent of:

SELECT DISTINCT(ID), TOP 2(Value)
FROM Table
GROUP BY ID
ORDER BY Order DESC

but I couldn't find an aggregate function that would do this for me, and I couldn't figure out a way to use LIMIT.  

Any ideas?  I hope this is clear enough.Start Free Trial
[+][-]08.28.2006 at 09:28PM PDT, ID: 17409151

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2006 at 01:59AM PDT, ID: 17410126

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2006 at 07:35AM PDT, ID: 17412151

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.29.2006 at 07:54AM PDT, ID: 17412302

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2006 at 08:51AM PDT, ID: 17412785

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2006 at 02:16PM PDT, ID: 17415296

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2006 at 02:35PM PDT, ID: 17415460

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.29.2006 at 10:15PM PDT, ID: 17417407

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.30.2006 at 12:36AM PDT, ID: 17418010

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MySQL
Tags: top, n, aggregate, function
Sign Up Now!
Solution Provided By: hans_vd
Participating Experts: 3
Solution Grade: A
 
 
[+][-]08.30.2006 at 01:32AM PDT, ID: 17418309

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]08.30.2006 at 06:37AM PDT, ID: 17420221

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32