将具有重复列的多行分散到postgresql中的单个唯一行
Scatter multiple rows having duplicate columns to single unique row in postgresql
如何在 sql/postgresql 中将多个重复行分散为一行。
例如--->
让我得到 3 行
col1 col2 col3
-------------------
11 test rat
11 test cat
11 test test
我想要这样的东西
col1 col2 col3 col4
------------------------
11 test rat cat
和lodash中的groupby一样。但是如何在 postgresql 查询中实现相同的功能?
您正在寻找 crosstab
postgres=# create table ab (col1 text, col2 text, col3 text);
CREATE TABLE
postgres=# insert into ab values ('t1','test','cat'),('t1','test','rat'),('t1','test','test');
INSERT 0 3
postgres=# select * from crosstab('select col1,col2,col3 from ab') as (col1 text, col2 text, col3 text, col4 text);
col1 | col2 | col3 | col4
------+------+------+------
t1 | cat | rat | test
(1 row)
披露:我为 EnterpriseDB (EDB)
工作
如何在 sql/postgresql 中将多个重复行分散为一行。
例如--->
让我得到 3 行
col1 col2 col3
-------------------
11 test rat
11 test cat
11 test test
我想要这样的东西
col1 col2 col3 col4
------------------------
11 test rat cat
和lodash中的groupby一样。但是如何在 postgresql 查询中实现相同的功能?
您正在寻找 crosstab
postgres=# create table ab (col1 text, col2 text, col3 text);
CREATE TABLE
postgres=# insert into ab values ('t1','test','cat'),('t1','test','rat'),('t1','test','test');
INSERT 0 3
postgres=# select * from crosstab('select col1,col2,col3 from ab') as (col1 text, col2 text, col3 text, col4 text);
col1 | col2 | col3 | col4
------+------+------+------
t1 | cat | rat | test
(1 row)
披露:我为 EnterpriseDB (EDB)
工作