mySQL - 每天显示最新条目,包括小数点和小数部分

时间:2015-10-16 18:24:48

标签: mysql

我正在使用簧片触点记录我的燃气表,并配有1线计数器。每次值更改时,相应值都会绘制到MySQL数据库中,并在每天午夜重置。出于评估目的,我希望获得每天最高(也是最新)的价值。 MySQL表如下所示:

+---------------------+---------+---------+-----------------+----------+-------+------+
| TIMESTAMP           | DEVICE  | TYPE    | EVENT           | READING  | VALUE | UNIT |
+---------------------+---------+---------+-----------------+----------+-------+------+
| 2015-10-16 01:51:04 | owc_gas | OWCOUNT | B_energy: 4.99  | B_energy | 4.99  |      |
| 2015-10-16 01:31:04 | owc_gas | OWCOUNT | B_energy: 3.99  | B_energy | 3.99  |      |
| 2015-10-16 01:11:04 | owc_gas | OWCOUNT | B_energy: 2.99  | B_energy | 2.99  |      |
| 2015-10-16 00:55:04 | owc_gas | OWCOUNT | B_energy: 2.00  | B_energy | 2.00  |      |
| 2015-10-16 00:41:04 | owc_gas | OWCOUNT | B_energy: 1.00  | B_energy | 1.00  |      |
| 2015-10-16 00:01:04 | owc_gas | OWCOUNT | B_energy: 0.00  | B_energy | 0.00  |      |
| 2015-10-15 22:33:04 | owc_gas | OWCOUNT | B_energy: 66.85 | B_energy | 66.85 |      |
| 2015-10-15 22:11:04 | owc_gas | OWCOUNT | B_energy: 65.85 | B_energy | 65.85 |      |
| 2015-10-15 21:49:03 | owc_gas | OWCOUNT | B_energy: 64.85 | B_energy | 64.85 |      |
| 2015-10-15 21:29:03 | owc_gas | OWCOUNT | B_energy: 63.85 | B_energy | 63.85 |      |
| 2015-10-15 21:09:03 | owc_gas | OWCOUNT | B_energy: 62.85 | B_energy | 62.85 |      |
| 2015-10-15 20:49:03 | owc_gas | OWCOUNT | B_energy: 61.86 | B_energy | 61.86 |      |
+---------------------+---------+---------+-----------------+----------+-------+------+

在stackoverflow的一些已经回答的问题的帮助下,我设法使用以下查询每天获取最新值:

SELECT   DATE(TIMESTAMP), MAX(CAST(VALUE AS UNSIGNED))
FROM     history t
where    READING="B_energy" AND DEVICE="owc_gas"
GROUP BY DATE(TIMESTAMP)

不幸的是,结果输出缺少小数点和以下小数部分值。

+-----------------+------------------------------+
| DATE(TIMESTAMP) | MAX(CAST(VALUE AS UNSIGNED)) |
+-----------------+------------------------------+
| 2015-10-02      |                            8 |
| 2015-10-03      |                           17 |
| 2015-10-04      |                           15 |
| 2015-10-05      |                           11 |
| 2015-10-06      |                            6 |
| 2015-10-07      |                           10 |
| 2015-10-08      |                           14 |
| 2015-10-09      |                           16 |
| 2015-10-10      |                           22 |
| 2015-10-11      |                           37 |
| 2015-10-12      |                           51 |
| 2015-10-13      |                           65 |
| 2015-10-14      |                           63 |
| 2015-10-15      |                           66 |
| 2015-10-16      |                           47 |
+-----------------+------------------------------+

我尝试了不同的转换和强制转换功能无效,我不明白为什么查询不尊重整个十进制值。

可悲的是,此时我已经基本了解了MySQL和查询。我能做点什么吗?

2 个答案:

答案 0 :(得分:0)

SELECT   DATE(TIMESTAMP), MAX(VALUE)
FROM     history t
where    READING="B_energy" AND DEVICE="owc_gas"
GROUP BY DATE(TIMESTAMP)

答案 1 :(得分:0)

在这里给出了一些提示,我碰巧找到了解决方案。问题是数据类型是varchar 32.这是因为其他设备具有“打开”或“关闭”等值。

以下查询的工作方式类似于魅力:

SELECT DATE(TIMESTAMP), MAX(CAST(VALUE AS decimal(10,2))) FROM history t where READING="B_energy" AND DEVICE="owc_gas" GROUP BY DATE(TIMESTAMP)

输出最终如下所示:

+-----------------+-----------------------------------+
| DATE(TIMESTAMP) | MAX(CAST(VALUE AS decimal(10,2))) |
+-----------------+-----------------------------------+
| 2015-10-02      |                              8.55 |
| 2015-10-03      |                             17.10 |
| 2015-10-04      |                             15.39 |
| 2015-10-05      |                             11.97 |
| 2015-10-06      |                              6.84 |
| 2015-10-07      |                             10.26 |
| 2015-10-08      |                             14.54 |
| 2015-10-09      |                             16.96 |
| 2015-10-10      |                             22.95 |
| 2015-10-11      |                             37.91 |
| 2015-10-12      |                             51.88 |
| 2015-10-13      |                             65.85 |
| 2015-10-14      |                             63.85 |
| 2015-10-15      |                             66.85 |
| 2015-10-16      |                             51.88 |
| 2015-10-17      |                             28.93 |
+-----------------+-----------------------------------+

这个查询可能不是最好的,但它对我有用。