function to add an element to an array and get all unique elements only
This commit is contained in:
parent
b34afce702
commit
27e8ea7025
23
postgres/jsonb_array_add_distinct.sql
Normal file
23
postgres/jsonb_array_add_distinct.sql
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
CREATE OR REPLACE FUNCTION public.jsonb_array_add_distinct(_arr jsonb, _add text) RETURNS jsonb AS
|
||||||
|
$$
|
||||||
|
DECLARE
|
||||||
|
_ret jsonb;
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
|
||||||
|
SELECT
|
||||||
|
jsonb_agg(DISTINCT x.ae)
|
||||||
|
INTO
|
||||||
|
_ret
|
||||||
|
FROM
|
||||||
|
(
|
||||||
|
SELECT jsonb_array_elements_text(_arr) ae
|
||||||
|
UNION ALL
|
||||||
|
SELECT _add ae
|
||||||
|
) x;
|
||||||
|
|
||||||
|
RETURN _ret;
|
||||||
|
|
||||||
|
END;
|
||||||
|
$$
|
||||||
|
language plpgsql
|
Loading…
Reference in New Issue
Block a user