gpt4 book ai didi

mysql - sql - 如何对值进行分组?

转载 作者:行者123 更新时间:2023-11-29 07:38:03 24 4
gpt4 key购买 nike

我有一张 table ,这样:

> SELECT * FROM tempLog WHERE DATE(ts) = CURDATE() LIMIT 9;

+-------+---------------------+----------+-------+-------+
| id | ts | sensorid | value | event |
+-------+---------------------+----------+-------+-------+
| 11020 | 2017-12-15 00:00:01 | 3 | 18.1 | cron |
| 11021 | 2017-12-15 00:00:01 | 4 | 14.8 | cron |
| 11022 | 2017-12-15 00:00:01 | 5 | 1.3 | cron |
| 11023 | 2017-12-15 00:02:02 | 3 | 18.1 | cron |
| 11024 | 2017-12-15 00:02:02 | 4 | 14.8 | cron |
| 11025 | 2017-12-15 00:02:02 | 5 | 1.3 | cron |
+-------+---------------------+----------+-------+-------+

是否可以构造一个查询,以便我得到

+---------------------+---------+---------+---------+
| ts | sensor3 | sensor4 | sensor5 |
+---------------------+---------+---------+---------+
| 2017-12-15 00:00:01 | 18.1 | 14.8 | 1.3 |
| 2017-12-15 00:02:02 | 18.1 | 14.8 | 1.3 |
+---------------------+---------+---------+---------+

也就是说,选择所有不同的“时间戳”并从当时的不同传感器获取每个值。

传感器稍后会更多,所以我必须考虑到这一点。他们都将在相同的“时间戳”进行轮询。

谢谢/乔恩

最佳答案

select ts, 
max(case when sensorid = 3 then value else 0 end) as sensor3,
max(case when sensorid = 4 then value else 0 end) as sensor4,
max(case when sensorid = 5 then value else 0 end) as sensor5
from your_table
group by ts

关于mysql - sql - 如何对值进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47838086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com