简体   繁体   English

重塑Postgres桌子,长到宽

[英]Reshape Postgres table, long to wide

Considering the following time series data in Postgres 9.5.1, I am trying to reshape to an array(int) |time_to_failure(double) as new table 考虑到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)

So that a row in new table would be 这样新表中的一行将是

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

I have some of the parts but am stuck on SELECT to achieve this result. 我有一些部分,但被困在SELECT上以实现此结果。 Any help greatly appreciated. 任何帮助,不胜感激。

You want array_agg ( docs ) with a group by: 您希望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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM