How can I select rows from MySQL based on max value of a one column and grouping two other columns? The query needs to group by code, group by element_id with where clause (element_id=1.B.1.b) and max(data_value).
code element_id data_value
11-1011.00 1.B.1.a 1.33
11-1011.00 1.B.1.b 2.00
11-1012.00 1.B.1.a 2.67
11-1012.00 1.B.1.b 3.67
11-1012.00 1.B.1.C 7.00
I have tried this:
SELECT * FROM (select
max(`data_value`) as maxID, `code`, `element_id`
from
table_name
GROUP BY `code`, `element_id`
ORDER BY maxID desc) abc
GROUP BY `code`
in that table i have lots of data. its a sample.
Here you can see more clear:
I need result :
11-1011.00 1.B.1.b 2.00
11-1012.00 1.B.1.c 7.00
this result for without where clasuse.
if using where clause i want only one result that match with element_id(1.B.1.b) that i have:
11-1011.00 1.B.1.b 2.00