au_stan
au_stan

Reputation: 4191

MAX/MIN giving me wrong values in mysql

I am trying to get the MAX() or the MIN() value of a dataset.

Sample data:

equipment_id, value, updateTimestamp

77, 81.57, 2013-05-28 12:00:00
77, 89.56, 2013-05-28 13:00:00
77, 92.76, 2013-05-28 14:00:00
77, 94.15, 2013-05-28 15:00:00
77, 97.66, 2013-05-28 16:00:00
77, 95.07, 2013-05-28 17:00:00
77, 95.15, 2013-05-28 18:00:00
77, 92.90, 2013-05-28 19:00:00
77, 96.16, 2013-05-28 20:00:00
77, 99.56, 2013-05-28 21:00:00
77, 101.67, 2013-05-28 22:00:00
77, 103.09, 2013-05-28 23:00:00
77, 103.34, 2013-05-29 00:00:00
77, 100.24, 2013-05-29 01:00:00
77, 99.66, 2013-05-29 02:00:00
77, 99.86, 2013-05-29 03:00:00
77, 98.38, 2013-05-29 04:00:00
77, 97.97, 2013-05-29 05:00:00
77, 98.06, 2013-05-29 06:00:00
77, 96.23, 2013-05-29 07:00:00
77, 95.92, 2013-05-29 08:00:00
77, 98.89, 2013-05-29 09:00:00
77, 97.73, 2013-05-29 10:00:00
77, 85.95, 2013-05-29 11:00:00
77, 73.72, 2013-05-29 12:00:00
77, 62.60, 2013-05-29 13:00:00

The following query will give me back 100.24

SELECT dv.equipment_id, ROUND(MIN(dv.value), 2) as value     
FROM b_datavalues dv
INNER JOIN b_equipment e ON e.equipment_id = dv.equipment_id
INNER JOIN b_datacenter d ON d.datacenter_id = e.datacenter_id
WHERE dv.equipment_id IS NOT NULL
AND dv.updateTimestamp BETWEEN '2013-05-28 11:45:00' and '2013-05-29 14:30:00'
AND d.name = 'rr'
AND e.name = 'hygrometer'

The correct answer should in fact be 62.60. For max I get a value of 99.86. It would appear that MIN() and MAX() are evaluating the first number only. Any idea how to resolve this?

Upvotes: 1

Views: 1285

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

The value appears to be stored as a string, rather than as a number. Try this instead:

SELECT dv.equipment_id, ROUND(MIN(cast(dv.value as decimal(5, 2))), 2) as value 

Upvotes: 3

Related Questions