SQL乘列值过滤行,并按过滤值创建新列

发布于 2025-01-26 01:18:32 字数 5414 浏览 2 评论 0原文

我在DB中的一项任务有问题 我正在将数据从plc加载到db(mysql),并且它们被读取,因为

id,timestamp,name,value.

我必须以形式进行读取:

timestamp,value1, value2, value 3 etc

值需要通过名称值来完成。

我的实际选择结果

IDTIMESTAMP名称
12022-02-16 16:38:49PT_TS121.5
22022-02-1638:38:49 PT_TS223.2
32022-02-16::38:38: 49
1616: 38 02-16 16:38:49PT_TS422.2
52022-02-16 16:38:50PT_TS521.0
62022-02-16 1623.2 7:38:38 :
:2022-16PT_TS638
502022-02-16 16:38:50PT_TS822.2
92022-02-16 16:38:30pt_ts_med21.825
102022-02-16 16:38:50CCL_RH137.8514
112022-02-16CCL_RH137.7514
122022-02-16 16:38:30VAHU2_SETPOINT27.0
132022-02-16 16:40:25PT_TS121.5
142022-02-02-16 16:40:25PT_TS2 PT_TS223.2 23.2
152022-02-02-16 16 16 16 16:40: 25PT_TS321.0
162022-02-16 16:40:25PT_TS422.2
172022-02-16 16:40:2521.018
2022-02-16PT_TS525:
4016:40: 40:25PT_TS720.3
2016:40:25PT_TS822.2
2116:40:25PT_TS_MED21.825
2022-02-162022-02-1621.825
222022-02-02-16 16:40:25CCL_RH237.7697
242022-02-16 16:40:25VAHU2_SETPOINT27.0

我想拥有这样的东西:

|TimeStamp           |PT_TS1 |PT_TS2 |PT_TS3 |PT_TS4 |PT_TS5 |PT_TS6 |PT_TS6 |PT_TS7 |PT_TS8 |

I have a problem with one task in a DB
I'm loading data from plc to DB (mySql), and they are read as

id,timestamp,name,value.

I have to make it in form:

timestamp,value1, value2, value 3 etc

filtering for value need to be done by name value

My actual select result

IDTimestampNameValue
12022-02-16 16:38:49PT_TS121.5
22022-02-16 16:38:49PT_TS223.2
32022-02-16 16:38:49PT_TS321.0
42022-02-16 16:38:49PT_TS422.2
52022-02-16 16:38:50PT_TS521.0
62022-02-16 16:38:50PT_TS623.2
72022-02-16 16:38:50PT_TS720.3
82022-02-16 16:38:50PT_TS822.2
92022-02-16 16:38:50PT_TS_med21.825
102022-02-16 16:38:50Ccl_RH137.8514
112022-02-16 16:38:50Ccl_RH237.7514
122022-02-16 16:38:50vAhu2_SetPoint27.0
132022-02-16 16:40:25PT_TS121.5
142022-02-16 16:40:25PT_TS223.2
152022-02-16 16:40:25PT_TS321.0
162022-02-16 16:40:25PT_TS422.2
172022-02-16 16:40:25PT_TS521.0
182022-02-16 16:40:25PT_TS623.2
192022-02-16 16:40:25PT_TS720.3
202022-02-16 16:40:25PT_TS822.2
212022-02-16 16:40:25PT_TS_med21.825
222022-02-16 16:40:25Ccl_RH137.8697
232022-02-16 16:40:25Ccl_RH237.7697
242022-02-16 16:40:25vAhu2_SetPoint27.0

I would want to have something like this:

|TimeStamp           |PT_TS1 |PT_TS2 |PT_TS3 |PT_TS4 |PT_TS5 |PT_TS6 |PT_TS6 |PT_TS7 |PT_TS8 |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2025-02-02 01:18:32

好的,所以我设法做了这样的事情:

SELECT CONVERT('timestamp'(16), @date ,20) AS datetime
group_concat(if(lab_ccl_monitor.Name='PT_TS1',Value, NULL)) as T1,
group_concat(if(lab_ccl_monitor.Name='PT_TS2',Value, NULL)) as T2,
group_concat(if(lab_ccl_monitor.Name='PT_TS3',Value, NULL)) as T3,
group_concat(if(lab_ccl_monitor.Name='PT_TS4',Value, NULL)) as T4,
group_concat(if(lab_ccl_monitor.Name='PT_TS5',Value, NULL)) as T5,
group_concat(if(lab_ccl_monitor.Name='PT_TS6',Value, NULL)) as T6,
group_concat(if(lab_ccl_monitor.Name='PT_TS7',Value, NULL)) as T7,
group_concat(if(lab_ccl_monitor.Name='PT_TS8',Value, NULL)) as T8
from
lab_ccl_monitor
group by
Timestamp ;

它有效,但是现在我有问题,我需要在最近的时间戳上加入行。有时我的时间戳在一个周期中是不同的。我的意思是,有54秒和第二个55 s。有什么方法

Okay so i manage to do something like that:

SELECT CONVERT('timestamp'(16), @date ,20) AS datetime
group_concat(if(lab_ccl_monitor.Name='PT_TS1',Value, NULL)) as T1,
group_concat(if(lab_ccl_monitor.Name='PT_TS2',Value, NULL)) as T2,
group_concat(if(lab_ccl_monitor.Name='PT_TS3',Value, NULL)) as T3,
group_concat(if(lab_ccl_monitor.Name='PT_TS4',Value, NULL)) as T4,
group_concat(if(lab_ccl_monitor.Name='PT_TS5',Value, NULL)) as T5,
group_concat(if(lab_ccl_monitor.Name='PT_TS6',Value, NULL)) as T6,
group_concat(if(lab_ccl_monitor.Name='PT_TS7',Value, NULL)) as T7,
group_concat(if(lab_ccl_monitor.Name='PT_TS8',Value, NULL)) as T8
from
lab_ccl_monitor
group by
Timestamp ;

and it works, but now i have problem, that i need join rows by nearest timestamp. Sometimes my timestamp is diffrent in one cycle. i mean, that one have 54s and second 55 s. is any way to do that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文