mysql - 在SQL中选择交叉数据

标签 mysql sql select

我有一个表格如下:

id      int(20)
ip      varchar(25)
temp    float
batt    int(4)
date    datetime

此表由收集数据的传感器提供,每 15 秒填充一次表。

1   fe80::212:4b00:60d:b27f 17.62   3256    2017-03-20 01:58:31
2   fe80::212:4b00:60d:6215 19.524  3264    2017-03-20 01:58:43
3   fe80::212:4b00:60d:b27f 17.62   3256    2017-03-20 01:58:46
4   fe80::212:4b00:60d:6215 19.524  3263    2017-03-20 01:58:58
5   fe80::212:4b00:60d:b27f 17.62   3256    2017-03-20 01:59:01

我希望通过传感器 (ip) 和按小时 (date) 获取平均温度 (temp)

类似下面的输出:

           IP              00:00   01:00   02:00   ... 
fe80::212:4b00:60d:b27f    18.16   18.20   18.23   ...
fe80::212:4b00:60d:6215    19.54   20.12   20.30   ...

知道如何在 mysql 中获取交叉数据输出吗?

最佳答案

您可以使用条件聚合:

select ip,
       avg(case when hour(date) = 00 then temp end) as temp_00,
       avg(case when hour(date) = 01 then temp end) as temp_01,
       avg(case when hour(date) = 02 then temp end) as temp_02,
       . . .
from t
where date >= '2017-03-20' and
      date < '2017-03-21'
group by ip;

关于mysql - 在SQL中选择交叉数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42950479/

相关文章:

MySQL:如何列出一列并对另一列求和?

php - WordPress 批量产品上传 (woocommerce) - 650K

PHP PDO 选择查询不返回任何结果

mysql - 如何在mysql中返回json字符串的值

MySQL:同一查询中的两个移动平均线?

SQL Server 临时表

c# - 离线应用-数据库

SQL Server CE ORM

mysql - Laravel-从下拉列表中选择值后自动填充输入

mysql - 为什么mysql 'select where clause'查询错误?