修改MySQL表的显示
Hi i have a mysql table like this:
TagName | DateTime | Value |
---|---|---|
'SGHAST_0001_Temp' | '2022-06-03 15:53:18.2400000' | '7.0' |
'SGHAST_0001_HUMIDITY' | '2022-06-03 15:53:18.2400000' | '80.0' |
'SGHAST_0002_Temp ' | '2022-06-03 15:53:18.2400000''17 | .0''sghast_0002_humisity''2022-06-03 |
如何将桌子分为下面的 | 15:53:53:18.2400000''50 | .0 .0' |
一行,
tagName | 温度 | 湿度 |
---|---|---|
sghast.0001'7.0''80 | '17 | .0'sghast.0002 |
.0 | .0''50 | ' |
我还能有代码行更改为此而不是建议吗?因为我是MySQL的新手。但是,任何帮助仍然值得赞赏!
到目前为止,我的代码:
SELECT DateTime,
SUM(CASE WHEN skynet_msa.testingintern.TagName LIKE 'Temp%'
THEN value
ELSE 0 END) as Temperature,
SUM(CASE WHEN skynet_msa.testingintern.TagName LIKE 'HUMIDITY%'
THEN value
ELSE 0 END) as Humidity,
FROM skynet_msa.testingintern
GROUP BY DateTime
LIMIT 0, 50000;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查询的主要问题是您要在“ dateTime ”字段上分组,该字段与每一行相等,并且无法识别所需的组。确定该组的内容实际上是“ tagName ”字段和“ datetime ”的第一部分的组合。
由于我们不能直接使用“ tagName ”,因此我们可以提取有趣的部分(例如
'sghast_000< n>'
来自'sghast_000< n> fieldName>'
)使用mysql substring_index 函数。当您传递字符(在我们的情况下_
)和索引i(在我们的情况下2
)时,它将在第一个字符和ITH之间检索字符串中的所有内容字符(在我们的情况下,'sghast_000< n>'
)。一旦我们能够建立我们的组,我们就可以在子句中使用
组中,并使用mysql
max
聚集功能以提取温度和湿度值时,仅当这些功能才出现在“ > tagName “字段。您可以在此 link 。
The main issue of your query is that you're grouping on the "DateTime" field, which is equal for every row and does not identify the group you want. What identifies the group is really the combination of the first part of the "TagName" field and the "Datetime" together.
Since we can't use "TagName" directly, we can extract the interesting part (e.g.
'SGHAST_000<n>'
from'SGHAST_000<n>_<fieldname>'
) using MySQLSUBSTRING_INDEX
function. When you pass a character (in our case_
) and an index i (in our case2
), it will retrieve everything in the string between the first character and the ith character (in our case'SGHAST_000<n>'
).Once we are able to build our group, we can use that inside a
GROUP BY
clause, and use MySQLMAX
aggregation function to extract the values of temperature and humidity when and only when these occur inside the "TagName" field.You can play with it (and select what specific function returns) at this link.