如何基于Postgres中的另一列聚合一个列

时间:2018-11-16 10:36:57

标签: sql postgresql

我有一个数据库,其中有来自不同来源的公司的数据。我希望能够为特定公司信息(字段)选择数据,并将它们合并为每个公司的单个记录。我希望在不使用第三范式存储的情况下执行此操作,因此我仍然可以具有参照完整性。我也希望动态地执行合并操作,而无需为列进行特定的编码。

数据和查询示例:

 
create table test2.company(
identifier int not null
,name varchar(100) null
,marketcap int null --in millions
,field varchar(100) not null
);

insert into test2.company(identifier, name, marketcap,field) values
(1,'Apple',1, 'name')
,(1,'Aplle',1000000,'marketcap')
;

select * from test2.company;

-结果

----------------------------------------------
| identifier | name  | marketcap | field     |
| ---------- | ----- | --------- | --------- |
| 1          | Apple | 1         | name      |
| 1          | Aplle | 1000000   | marketcap |
----------------------------------------------

到目前为止,我想出了最好的方法:

with x1 as (select
    c.identifier
    ,case when c.field = 'name' then c.name else null end as name
    ,case when c.field = 'marketcap' then marketcap else null end as marketcap
    from test2.company c
)
, x2 as (select 
    x1.identifier
    ,string_agg(x1.name,'') as name
    ,sum(x1.marketcap) as marketcap
    from x1
    group by x1.identifier
)
select * from x2;

-结果

----------------------------------
| identifier | name  | marketcap |
| ---------- | ----- | --------- |
| 1          | Apple | 1000000   |
----------------------------------

如您所见,我不得不为这些列专门编码。在数据类型是数字的地方,我不得不使用sum和vs string_agg。

是否有某种通用的方法?

1 个答案:

答案 0 :(得分:0)

我想这就是它的通用性-

with x1 as (select
    c.identifier
    ,c.field
    ,string_agg(c.name, '') as name
    ,sum(c.marketcap) as marketcap
    from test2.company c
    group by c.identifier, c.field
)
select x.identifier, 
       (select name from x1 a where a.field = 'name' and a.identfier = x.identifier) as name, 
       (select marketcap from x1 b where b.field = 'marketcap' and b.identfier = x.identifier) as marketcap 
       from x1 x;