Postgres查询应返回一个数组而不是字符串数组

时间:2019-03-07 10:44:56

标签: postgresql postgresql-9.2

强文本,我遇到以下问题: 给定两个表的联系人和组织:

WITH contacts(oe_id, name, email, person_id) AS (VALUES
    (1, 'Mark', 'm.smith@test.nl', 19650728),
    (2, 'Tom', 't.b.smith@test.nl', 20010627), 
    (1, 'Frank', 'f.j.smith@test.nl', 20040709), 
    (3, 'Petra', 'p.ringenaldus@test.nl', 19700317), 
    (3, 'Paul', 'p.m.sprengers@test.nl', 19681006)),
organisations(oe_id, name) AS (VALUES
    (1, 'Cardiology'), 
    (2, 'Neurology'), 
    (3, 'Dermatology'), 
    (4, 'Churgery'))

我想得到一个包含3列的表格:组织名称,组织ID和该组织的联系人数组。 每个数组元素也是一个包含联系人数据的数组。

首先,我创建了一个表,其中所有联系人列都被汇总到一个数组中。每元组行一个数组:

 WITH contacts(oe_id, name, email, person_id) AS (VALUES
    (1, 'Mark', 'm.smith@test.nl', 19650728), 
    (2, 'Tom', 't.b.smith@test.nl', 20010627), 
    (1, 'Frank', 'f.j.smith@test.nl', 20040709), 
    (3, 'Petra', 'p.ringenaldus@test.nl', 19700317), 
    (3, 'Paul', 'p.m.sprengers@test.nl', 19681006)),
organisations(oe_id, name) AS (VALUES
    (1, 'Cardiology'), 
    (2, 'Neurology'), 
    (3, 'Dermatology'), 
    (4, 'Churgery')),
contacts_aggregated(oe_id, cdata) AS (
    select oe_id, ARRAY[name, email, person_id::text] from contacts) 
select * from contacts_aggregated;

此结果为:

 oe_id |                 cdata
-------+---------------------------------------
     1 | {Mark,m.smith@test.nl,19650728}
     2 | {Tom,t.b.smith@test.nl,20010627}
     1 | {Frank,f.j.smith@test.nl,20040709}
     3 | {Petra,p.ringenaldus@test.nl,19700317}
     3 | {Paul,p.m.sprengers@test.nl,19681006}
(5 rows)

下一步是为每个组织ID汇总cdata(联系数据):

WITH contacts(oe_id, name, email, person_id) AS (VALUES
    (1, 'Mark', 'm.smith@test.nl', 19650728), 
    (2, 'Tom', 't.b.smith@test.nl', 20010627), 
    (1, 'Frank', 'f.j.smith@test.nl', 20040709), 
    (3, 'Petra', 'p.ringenaldus@test.nl', 19700317), 
    (3, 'Paul', 'p.m.sprengers@test.nl', 19681006)),
organisations(oe_id, name) AS (VALUES
    (1, 'Cardiology'), 
    (2, 'Neurology'), 
    (3, 'Dermatology'), 
    (4, 'Churgery')),
contacts_aggregated(oe_id, cdata) AS (
    select oe_id, ARRAY[name, email, person_id::text] from contacts),
contacts_for_organisations(oe_id, contacts) AS (
    SELECT organisations.oe_id, array_agg(contacts_aggregated.cdata::text) 
    FROM organisations
    JOIN contacts_aggregated USING(oe_id) 
    GROUP BY oe_id)
SELECT * FROM contacts_for_organisations;

结果如下:

 oe_id |                                      contacts
-------+------------------------------------------------------------------------------------
     1 | {"{Mark,m.smith@test.nl,19650728}","{Frank,f.j.smith@test.nl,20040709}"}
     2 | {"{Tom,t.b.smith@test.nl,20010627}"}
     3 | {"{Petra,p.ringenaldus@test.nl,19700317}","{Paul,p.m.sprengers@test.nl,19681006}"}
(3 rows)

如您所见,结果是一个数组。但是它的元素也应该是一个数组。代替数组,我得到一个内爆数组作为字符串。

我想要的Wat是这样的:

 oe_id |                                      contacts
-------+------------------------------------------------------------------------------------
     1 | {{Mark,m.smith@test.nl,19650728},{Frank,f.j.smith@test.nl,20040709}}
     2 | {{Tom,t.b.smith@test.nl,20010627}}
     3 | {{Petra,p.ringenaldus@test.nl,19700317},{Paul,p.m.sprengers@test.nl,19681006}}
(3 rows)

如果我删除对文本array_agg(contacts_aggregated.cdata :: text的转换,我会得到:

could not find array type for data type text[]

我忘记/做错了什么? Postgres:psql(9.2.24)和psql(9.6.10,服务器9.2.24)

如果我在postgres 9.6服务器上使用postgres客户端9.6运行代码,则一切正常。

1 个答案:

答案 0 :(得分:1)

我刚刚移至更高的postgres版本,现在一切正常。