MAX / MIN在mysql中给出了错误的值

时间:2013-05-29 19:21:10

标签: mysql max min

我正在尝试获取数据集的MAX()MIN()值。

示例数据:

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

以下查询将返回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'

正确答案应该是62.60。对于最大值,我得到99.86的值。 MIN()MAX()似乎只评估第一个数字。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

该值似乎存储为字符串,而不是数字。试试这个:

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