如何将多个integer类型的列附加到单个列中

时间:2018-04-19 12:49:07

标签: postgresql

以下是我的查询: -

select 
ou.organisationunitid as one,
ou2.organisationunitid as two,
ou3.organisationunitid as three,
ou4.organisationunitid as four,
ou5.organisationunitid as five,
ou6.organisationunitid as six,
ou7.organisationunitid as seven,
ou8.organisationunitid as eight,
ou9.organisationunitid as nine,
ou10.organisationunitid as ten ,
ou11.organisationunitid as eleven
from orgunitgroupmembers ougm
.
.
.
group by   ou.organisationunitid,ou2.organisationunitid,ou3.organisationunitid,ou4.organisationunitid,ou5.organisationunitid,ou6.organisationunitid,ou7.organisationunitid,ou8.organisationunitid,ou9.organisationunitid,ou10.organisationunitid,ou11.organisationunitid

这是输出: enter image description here

我想将所有这些整数类型的ID组合到一个列中,以便我可以将它提供给另一个查询。例如: select * from orgunit,其中id为<以上查询的输出

我尝试对concat进行字符串处理,但这不起作用,因为我需要将其提供给另一个查询。

那么,有没有办法实现这一目标?任何帮助将深表感谢。请提出一个解决方案,该解决方案兼容将生成的单列输出作为输入提供给另一个查询。

注意:我在受限制的环境中工作,因此我无法使用任何“功能”或插件。

谢谢

好的,主要编辑:

这是小提琴 - &gt; https://www.db-fiddle.com/f/tKYF4jeDm8B5cWLdhWav3o/0

我想要做的是,我想在层次结构中获得orgunitgroupmembers以及他们所有的孩子。自我联接之间的关系是父子关系。

同时粘贴创建表和查询:

     CREATE TABLE organisationunit (
  organisationunitid integer,
  parentid integer

);

INSERT INTO organisationunit (organisationunitid,parentid) VALUES (1,NULL);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (2,1);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (3,2);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (4,3);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (5,3);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (6,3);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (7,3);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (8,3);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (9,3);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (10,4);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (11,4);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (12,4);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (13,5);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (14,5);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (15,5);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (16,5);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (17,6);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (18,6);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (19,6);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (20,7);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (21,7);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (22,7);
INSERT INTO organisationunit (organisationunitid,parentid) VALUES (23,7);


CREATE TABLE orgunitgroupmember (
  orgunitgroupid integer,
  organisationunitid integer  
);

INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (1,10);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (1,11);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (1,5);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (1,6);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (1,8);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (1,22);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (1,3);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (2,15);
INSERT INTO orgunitgroupmember (orgunitgroupid,organisationunitid) VALUES (3,22);

CREATE TABLE orgunitgroup (
  orgunitgroupid integer
);

INSERT INTO orgunitgroup (orgunitgroupid) VALUES (1);

完整查询:

select 
ou.organisationunitid as one,
ou2.organisationunitid as two,
ou3.organisationunitid as three,
ou4.organisationunitid as four,
ou5.organisationunitid as five,
ou6.organisationunitid as six,
ou7.organisationunitid as seven,
ou8.organisationunitid as eight,
ou9.organisationunitid as nine,
ou10.organisationunitid as ten ,
ou11.organisationunitid as eleven
from orgunitgroupmember ougm
inner join organisationunit ou on ou.organisationunitid =  ougm.organisationunitid
left join organisationunit ou2 on ou.organisationunitid =  ou2.parentid
left join organisationunit ou3 on ou2.organisationunitid =  ou3.parentid
left join organisationunit ou4 on ou3.organisationunitid =  ou4.parentid
left join organisationunit ou5 on ou4.organisationunitid =  ou5.parentid
left join organisationunit ou6 on ou5.organisationunitid =  ou6.parentid
left join organisationunit ou7 on ou5.organisationunitid =  ou7.parentid
left join organisationunit ou8 on ou5.organisationunitid =  ou8.parentid
left join organisationunit ou9 on ou5.organisationunitid =  ou9.parentid
left join organisationunit ou10 on ou5.organisationunitid =  ou10.parentid
left join organisationunit ou11 on ou5.organisationunitid =  ou11.parentid

inner join orgunitgroup oug on oug.orgunitgroupid = ougm.orgunitgroupid
where oug.orgunitgroupid in (1)
group by ou.organisationunitid,ou2.organisationunitid,ou3.organisationunitid,ou4.organisationunitid,ou5.organisationunitid,ou6.organisationunitid,ou7.organisationunitid,ou8.organisationunitid,ou9.organisationunitid,ou10.organisationunitid,ou11.organisationunitid

2 个答案:

答案 0 :(得分:1)

你可以尝试一个懒惰的黑客,例如你有:

t=# with c(x,y,z,w) as (values(1,null,3,4),(5,6,7,null))
select *,translate(regexp_replace(c::text,',([^0-9])',',null\1','g'),'()','{}')::int[] from c;
 x | y | z | w |  translate
---+---+---+---+--------------
 1 |   | 3 | 4 | {1,NULL,3,4}
 5 | 6 | 7 |   | {5,6,7,NULL}
(2 rows)

所以只是汇总它们:

t=# with c(x,y,z,w) as (values(1,null,3,4),(5,6,7,null))
,un as (select unnest(translate(regexp_replace(c::text,',([^0-9])',',null\1','g'),'()','{}')::int[]) from c)
select array_agg(unnest) from un;
        array_agg
-------------------------
 {1,NULL,3,4,5,6,7,NULL}
(1 row)

最后使用any

t=# with c(x,y,z,w) as (values(1,null,3,4),(5,6,7,null))
,un as (select unnest(translate(regexp_replace(c::text,',([^0-9])',',null\1','g'),'()','{}')::int[]) from c)
, ag as (select array_agg(unnest) from un)
select 'there' from ag where 3 = any(array_agg);
 ?column?
----------
 there
(1 row)

t=# with c(x,y,z,w) as (values(1,null,3,4),(5,6,7,null))
,un as (select unnest(translate(regexp_replace(c::text,',([^0-9])',',null\1','g'),'()','{}')::int[]) from c)
, ag as (select array_agg(unnest) from un)
select 'there' from ag where 2 = any(array_agg);
 ?column?
----------
(0 rows)

当然“铸造”元组到阵列不能可靠并且用于产品。你可能需要为所有字段命名。但是这样的猴子黑客可以保存你的打字以便进行简单的检查

答案 1 :(得分:0)

看起来你实际上需要一个可以用作子查询的递归查询:

with recursive org_units as (
  select ou.organisationunitid, 1 as level
  from orgunitgroupmember ougm
    join organisationunit ou on ou.organisationunitid =  ougm.organisationunitid
    join orgunitgroup oug on oug.orgunitgroupid = ougm.orgunitgroupid
  where oug.orgunitgroupid in (1)

  union all

  select ch.organisationunitid, p.level + 1
  from organisationunit ch 
    join org_units p on ch.parentid = p.organisationunitid
)
select ... 
from ..... -- << here is your main query 
where some_id in (select organisationunitid from org_units);
                  ^
                  | this is where you re-use the result of the recursive query

这也有一个好处,就是您不需要知道层次结构有多少级别。