本文介绍了Perl + PostgreSQL-选择性的列到行转置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种使用Perl进一步处理PostgreSQL输出的方法。如果通过PostgreSQL有更好的方法,请告诉我。我基本上需要在文件中选择某些列(实时,值)来连接某些列以创建行,同时保留ID和CAT。

I'm trying to find a way to use Perl to further process a PostgreSQL output. If there's a better way to do this via PostgreSQL, please let me know. I basically need to choose certain columns (Realtime, Value) in a file to concatenate certains columns to create a row while keeping ID and CAT.

首次发布,所以请让我知道我是否错过了任何事情。

First time posting, so please let me know if I missed anything.

输入:

ID  CAT   Realtime  Value
A   1    time1       55
A   1    time2       57
B   1    time3       75
C   2    time4       60
C   3    time5       66
C   3    time6       67

输出:

ID  CAT  Time                   Values
A   1    time 1,time2           55,57
B   1    time3                  75
C   2    time4                  60
C   3    time5,time6            66,67


推荐答案

您可以像这样(使用数组列)在Postgres中最简单地做到这一点

You could do this most simply in Postgres like so (using array columns)

CREATE TEMP TABLE output AS SELECT
  id, cat, ARRAY_AGG(realtime) as time, ARRAY_AGG(value) as values
  FROM input GROUP BY id, cat;

然后从输出表中选择所需的内容。

Then select whatever you want out of the output table.

这篇关于Perl + PostgreSQL-选择性的列到行转置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 06:42