I have a table with three one dimensional arrays (and also a primary key and several other columns, but they do not matter). All of these arrays could be NULL
. The arrays do overlap quite often in each column: Many values are in several arrays. I now want a query which returns one row with three arrays being the distinct values of the whole array column.
Creating the test table like so
DROP TABLE IF EXISTS my_array_test;
CREATE TABLE IF NOT EXISTS my_array_test(id integer, my_txt text[], my_int1 integer[], my_int2 integer[]);
INSERT INTO my_array_test(id, my_txt, my_int1, my_int2) VALUES (1,'{text1,text2}','{1,2}','{21,22}');
INSERT INTO my_array_test(id, my_txt, my_int1, my_int2) VALUES (2,null,'{7,8}','{21,22}');
INSERT INTO my_array_test(id, my_txt, my_int1, my_int2) VALUES (3,'{text2,text4}',null,null);
INSERT INTO my_array_test(id, my_txt, my_int1, my_int2) VALUES (3,null,null,'{17,18}');
INSERT INTO my_array_test(id, my_txt, my_int1, my_int2) VALUES (4,'{text1,text2}','{1,2,3}','{21,22}');
INSERT INTO my_array_test(id, my_txt, my_int1, my_int2) VALUES (5,'{text1,text5}','{1,5}','{21,25}');
INSERT INTO my_array_test(id, my_txt, my_int1, my_int2) VALUES (6,null,null,null);
The result being something like
select * from my_array_test ;
id | my_txt | my_int1 | my_int2
----+---------------+---------+---------
1 | {text1,text2} | {1,2} | {21,22}
2 | | {7,8} | {21,22}
3 | {text2,text4} | |
3 | | | {17,18}
4 | {text1,text2} | {1,2,3} | {21,22}
5 | {text1,text5} | {1,5} | {21,25}
6 | | |
(7 rows)
The expected result would be {text1,text2,text4,text5},{1,2,7,8,2,5},{21,22,17,18,25}
(order within the array is not important.)
What I tried was a multiple lateral query like so:
SELECT
array_agg(DISTINCT t) AS text_array_result,
array_agg(DISTINCT i1) AS integer_array1_result,
array_agg(DISTINCT i2) AS integer_array2_result
FROM
my_array_test,
unnest(my_txt) AS t,
unnest(my_int1) AS i1,
unnest(my_int2) AS i2
However, that kills all values which are only in rows with NULL
arrays among them.
I also tried unnest(COALESCE(my_txt,'{}')) AS t,
and so on, but to no avail.