gpt4 book ai didi

SQL使每n行到一个单独的列

转载 作者:行者123 更新时间:2023-11-29 13:46:11 26 4
gpt4 key购买 nike

我有一个小问题。我的问题是我有一个表,其中只有一列包含传感器数据。每三行代表一个新样本。我现在的目标是将每三行转换为一个单独的列,如 3 x N 矩阵。我不知道我的 N 有多大。是否可以使用 SQL 执行此操作?

--值(value)观--
sensor_sampleA1
sensor_sampleA2
sensor_sampleA3
sensor_sampleB1
sensor_sampleB2
sensor_sampleB3
...
...
...
sensor_sampleN1
sensor_sampleN2
传感器样本N3

我想要这个。

--目标表--
样本A1 样本B1 样本C1 ... ... 样本N1
样本A2 样本B2 样本C2 ... ... 样本N2
样本A3 样本B3 样本C3 ... ... 样本N3

更新:我以上面的数字 3 为例。我有 11 个传感器,每秒可提供 11 个值。我的列是:sensor_data_id、axis、source、type、value 和 sample_id。

picture of the database

最佳答案

这是可能的解决方案之一。我使用了前 4 种度量类型 - 这应该可以说明这一点。

select
sample_id
,max(case when axis = 'X' and type = 'linear acceleration' then value else null end) as sensor1
,max(case when axis = 'Y' and type = 'linear acceleration' then value else null end) as sensor2
,max(case when axis = 'Z' and type = 'linear acceleration' then value else null end) as sensor3
,max(case when axis = 'X' and type = 'gyroscope ' then value else null end) as sensor4
from have
group by sample_id

关于SQL使每n行到一个单独的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48156817/

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