forked from rin-nas/postgresql-patterns-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_cat_agg.sql
More file actions
36 lines (33 loc) · 1.07 KB
/
array_cat_agg.sql
File metadata and controls
36 lines (33 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- PostgreSQL < 13
CREATE AGGREGATE public.array_cat_agg(anyarray) (
SFUNC = array_cat
,STYPE = anyarray
,INITCOND = '{}'
);
-- PostgreSQL 13+
CREATE AGGREGATE public.array_cat_agg(anycompatiblearray) (
SFUNC = array_cat
,STYPE = anycompatiblearray
,INITCOND = '{}'
);
--TEST1
SELECT id, public.array_cat_agg(words::text[])
FROM (VALUES
('1', '{"foo","bar","zap","bing"}'),
('2', '{"foo"}'),
('1', '{"bar","zap"}'),
('2', '{"bing"}'),
('1', '{"bing"}'),
('2', '{"foo","bar"}')) AS t(id, words)
GROUP BY id;
--TEST2 -- без функции array_cat_agg() можно обойтись, если немного переписать запрос!
SELECT id, array_agg(u.w) as words
FROM (VALUES
('1', '{"foo","bar","zap","bing"}'),
('2', '{"foo"}'),
('1', '{"bar","zap"}'),
('2', '{"bing"}'),
('1', '{"bing"}'),
('2', '{"foo","bar"}')) AS t(id, words),
unnest(words::text[]) as u(w)
GROUP BY id;