2016-06-23 39 views
1

鑑於以下起始數據:保持一致的PostgreSQL陣列列索引而串聯陣列列具有空柱處理時值

CREATE TABLE t1 AS 
    SELECT generate_series(1, 20) AS id, 
    (SELECT array_agg(generate_series) FROM generate_series(1, 6)) as array_1; 

CREATE TABLE t2 AS 
    SELECT generate_series(5, 10) AS id, 
    (SELECT array_agg(generate_series) FROM generate_series(7, 10)) as array_2; 

CREATE TABLE t3 AS 
    SELECT generate_series(8, 15) AS id, 
    (SELECT array_agg(generate_series) FROM generate_series(11, 15)) as array_3; 

我想這樣做的外幾個表之間的連接,每一個固定長度數組列在給定表中是統一的,但可能因表與表之間的不同而不同(如上例所示),將每個表中的數組列連接成一個大數組列。我想知道是否有一種有效或直接的方法在新的組合列中保持一致的索引,用NULL值的數組替換NULL列值(由外部聯接導致),以便最終的數組列具有統一的長度。與上例不同,在我的實際使用情況中,我不知道每個表的數組列先驗的長度,只是它在整個表中具有統一的長度。換句話說,代替此查詢:

SELECT id, (array_1 || array_2 || array_3) AS combined_array FROM 
t1 LEFT OUTER JOIN t2 USING(id) LEFT OUTER JOIN t3 USING (id); 

主要生產:

id |   combined_array 
----+--------------------------------------- 
1 | {1,2,3,4,5,6} 
2 | {1,2,3,4,5,6} 
3 | {1,2,3,4,5,6} 
4 | {1,2,3,4,5,6} 
5 | {1,2,3,4,5,6,7,8,9,10} 
6 | {1,2,3,4,5,6,7,8,9,10} 
7 | {1,2,3,4,5,6,7,8,9,10} 
8 | {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 
9 | {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 
10 | {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 
11 | {1,2,3,4,5,6,11,12,13,14,15} 
12 | {1,2,3,4,5,6,11,12,13,14,15} 
13 | {1,2,3,4,5,6,11,12,13,14,15} 
14 | {1,2,3,4,5,6,11,12,13,14,15} 
15 | {1,2,3,4,5,6,11,12,13,14,15} 
16 | {1,2,3,4,5,6} 
17 | {1,2,3,4,5,6} 
18 | {1,2,3,4,5,6} 
19 | {1,2,3,4,5,6} 
20 | {1,2,3,4,5,6} 
(20 rows) 

我想結果看起來像:

id |   combined_array 
----+--------------------------------------- 
1 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
2 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
3 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
4 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
5 | {1,2,3,4,5,6,7,8,9,10,NULL,NULL,NULL,NULL,NULL} 
6 | {1,2,3,4,5,6,7,8,9,10,NULL,NULL,NULL,NULL,NULL} 
7 | {1,2,3,4,5,6,7,8,9,10,NULL,NULL,NULL,NULL,NULL} 
8 | {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 
9 | {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 
10 | {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} 
11 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,11,12,13,14,15} 
12 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,11,12,13,14,15} 
13 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,11,12,13,14,15} 
14 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,11,12,13,14,15} 
15 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,11,12,13,14,15} 
16 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
17 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
18 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
19 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
20 | {1,2,3,4,5,6,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL} 
(20 rows) 

使每一行包含數組長度爲15.

+0

n長度?每行可以有不同的長度?你最大的價值呢?如果是的話,如何處理較短的行? –

+0

對不起,我的描述有點混亂。每個表都有一個在整個表中具有統一長度的數組列,但如果有意義的話,我不一定知道特定表的數組長度。所以表1可能有一個長度爲10的數組列,而表2可能有一個長度爲3的數組列。在我的用例中,我無法對這些值進行硬編碼。 –

回答

1

爲了回答我自己的問題,下面是我提出的查詢,似乎是做這項工作。它對我來說似乎並不特別優雅或有效,因此絕對可以接受其他答案。

SELECT id, (
    coalesce(array_1, array_fill(NULL::INT, 
    ARRAY[(SELECT max(array_length(array_1, 1)) FROM t1)])) || 
    coalesce(array_2, array_fill(NULL::INT, 
    ARRAY[(SELECT max(array_length(array_2, 1)) FROM t2)])) || 
    coalesce(array_3, array_fill(NULL::INT, 
    ARRAY[(SELECT max(array_length(array_3, 1)) FROM t3)])) 
) AS combined_array FROM 
t1 LEFT OUTER JOIN t2 USING(id) LEFT OUTER JOIN t3 USING (id);