Bug #29606 LAST ENTRY of a GROUP BY sql statement
Submitted: 6 Jul 2007 18:57 Modified: 20 Jul 2009 9:44
Reporter: numpsy beelzebub Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: General Severity:S4 (Feature request)
Version:5.1 OS:Any
Assigned to: CPU Architecture:Any
Tags: GROUP BY, last, LAST VALUE

[6 Jul 2007 18:57] numpsy beelzebub
Description:
if i select something using the group by funktion it is possible to get the first value, which is the value itself, the max, the min, the sum, the count and the avg - but impossible to get the last of a group

How to repeat:
example:

table: 
value - group_value
===================
1 - 1
2 - 1
3 - 1
4 - 1
5 - 2
6 - 2
7 - 2
8 - 2

sql:
SELECT value, MIN(value), MAX(value), COUNT(value) from tbl_name GROUP BY group_value,

Result:
1, 1, 4, 4
5, 5, 8, 4

how i get the last of the group? nothing found in source - no implementation, which shouldn't be so hard - isn't it?

some kind of FIRST(value), LAST(value) would do it!
1, 4
5, 8
[20 Jul 2009 9:44] Susanne Ebrecht
Many thanks for writing a feature request. Unfortunately, this is not a bug because you are already able to use ORDER BY here:

http://dev.mysql.com/doc/refman/5.1/en/order-by-optimization.html
[24 Jul 2009 3:12] John Chew
Was this bug really fixed in 5.1? In 5.0.45, even if you specify ORDER BY a column, if you select that column in a GROUP BY, the value you get is the one in the first row.
[24 Jul 2009 3:19] John Chew
I think if you number your records, you can use the approach in http://dev.mysql.com/doc/refman/5.0/en/example-maximum-column-group-row.html to answer the original question.