重塑Postgres表格,从长到宽

时间:2019-03-19 17:02:53

标签: sql postgresql

考虑到Postgres 9.5.1中的以下时间序列数据,我试图将其重塑为 array(int)| time_to_failure(double)作为新表

# SELECT acoustic_data, time_to_failure FROM lanl_train WHERE 
time_to_failure = '0.000795';
 acoustic_data | time_to_failure 
---------------+-----------------
             2 |        0.000795
             5 |        0.000795
             6 |        0.000795
             1 |        0.000795
             2 |        0.000795
             5 |        0.000795
             8 |        0.000795
             5 |        0.000795
             4 |        0.000795
             4 |        0.000795
             7 |        0.000795
             2 |        0.000795
             2 |        0.000795
             0 |        0.000795
             0 |        0.000795
             2 |        0.000795
             4 |        0.000795
             5 |        0.000795
             4 |        0.000795
(19 rows)

# SELECT ARRAY(SELECT acoustic_data FROM lanl_train WHERE 
time_to_failure = '0.000795');
                  array                  
-----------------------------------------
 {2,5,6,1,2,5,8,5,4,4,7,2,2,0,0,2,4,5,4}
(1 row)

这样,新表中的行将成为

acoustic_data(array) | time_to_failure(double)
----------------------------------------------
{2,5,6,1,2,5,8,5,4,4,7,2,2,0,0,2,4,5,4} | 0.000795

我有一些部分,但被困在SELECT上以实现此结果。任何帮助,不胜感激。

1 个答案:

答案 0 :(得分:1)

您希望array_aggdocs)与以下人员分组:

select
    array_agg(lanl_train.acoustic_data) as acoustic_data,
    lanl_train.time_to_failure
from
    lanl_train
where
    lanl_train.time_to_failure = '0.000795'
group by
    lanl_train.time_to_failure