Est-il possible de faire un 1 sur 1 concaténation tableau élément si j'ai une requête comme ceci:éléments du tableau Concaténer sur une table jointe PostgreSQL
EDIT: Les tableaux ont pas toujours le même nombre d'éléments. pourrait être que array1 a parfois 4 éléments et array2 8 éléments.
drop table if exists a;
drop table if exists b;
create temporary table a as (select 1 as id,array['a','b','c'] as array1);
create temporary table b as (select 1 as id,array['x','y','z'] as array2);
select
a.id,
a.array1,
b.array2,
array_concat--This has to be a 1 to 1 ordered concatenation (see
--example below)
from a
left join b on b.id=a.id
Ce que je voudrais obtenir ici est une concaténation paire des deux tableaux 1 et 2, comme ceci:
id array11 array2 array_concat
1 ['a','b','c'] ['d','e','f'] ['a-d','b-e','c-f']
2 ['x','y','z'] ['i','j','k'] ['x-i','y-j','z-k']
3 ...
J'ai essayé d'utiliser unnest mais je ne peux pas le faire fonctionner:
select
a.id,
a.array1,
b.array2,
array_concat
from table a
left join b on b.id=a.id
left join (select a.array1,b.array2, array_agg(a1||b2)
FROM unnest(a.array1, b.array2)
ab (a1, b2)
) ag on ag.array1=a.array1 and ag.array2=b.array2
;
EDIT:
Cela fonctionne pour une seule table:
SELECT array_agg(el1||el2)
FROM unnest(ARRAY['a','b','c'], ARRAY['d','e','f']) el (el1, el2);
++ Merci à https://stackoverflow.com/users/1463595/%D0%9D%D0%9B%D0%9E
EDIT:
Je suis venu à une solution très proche, mais il mêle quelques-unes des valeurs intermédiaires une fois que la concaténation entre des réseaux se fait, jamais moins j'ai encore besoin d'une solution parfaite ...
L'approche que j'utilise maintenant est:
1) Création d'une table sur la base 2 les différents 2) agrégeant en utilisant latérale:
create temporary table new_table as
SELECT
id,
a.a,
b.b
FROM a a
LEFT JOIN b b on a.id=b.id;
SELECT id,
ab_unified
FROM pair_sources_mediums_campaigns,
LATERAL (SELECT ARRAY_AGG(a||'[-]'||b order by grp1) as ab_unified
FROM (SELECT DISTINCT case when a null
then 'not tracked'
else a
end as a
,case when b is null
then 'none'
else b
end as b
,rn - ROW_NUMBER() OVER(PARTITION BY a,b ORDER BY rn) AS grp1
FROM unnest(a,b) with ordinality as el (a,b,rn)
) AS sub
) AS lat1
order by 1;
Side note: JSON correcte est avec '" 'pas' ' ' – maraca
Dans PostgreSQL, vous pouvez également définir la chaîne. contenant des tableaux comme: select array ['a', 'b', 'c'] comme array1; – johan855