MySQL Forums
Forum List  »  Newbie

Re: Help with minimum/group by
Posted by: Brendan Moran
Date: November 08, 2009 11:37AM

Suppose now that I want to make this query work for several "groups" in my table:

+---+-------+------+
| a | b | c |
+---+-------+------+
| 1 | 1 | 1 |
| 2 | 5.7 | 1 |
| 3 | 15.9 | 2 |
| 4 | 6.8 | 2 |
| 5 | 17 | 3 |
| 6 | -12.7 | 3 |
+---+-------+------+

This will not work:
SELECT a,b,c,abs(b-14.3) as d FROM closest c WHERE abs(b-14.3)=(SELECT MIN(ABS(b-14.3)) FROM closest GROUP BY c) GROUP BY c;

So then the correct syntax is to use a join to a subquery?

SELECT l.a,l.b,l.c FROM (SELECT MIN(ABS(14.3-b)) AS d,c FROM closest GROUP BY c) AS r
JOIN closest AS l ON d=ABS(l.b-14.3);

Options: ReplyQuote


Subject
Written By
Posted
November 08, 2009 12:04AM
Re: Help with minimum/group by
November 08, 2009 11:37AM
November 08, 2009 03:37PM
November 08, 2009 05:55PM
November 08, 2009 06:10PM
November 08, 2009 06:23PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.