Skip to content

Instantly share code, notes, and snippets.

@lossendae
Last active March 29, 2021 12:54
Show Gist options
  • Save lossendae/9568055671a2c1a0dccce6478e043b34 to your computer and use it in GitHub Desktop.
Save lossendae/9568055671a2c1a0dccce6478e043b34 to your computer and use it in GitHub Desktop.
Various POSTGRESQL helpers
with
test as (
select *
from (
values
(123, '{"key_1": {"value_1": 1}}'::jsonb),
(123, '{"key_2": {"value_2": 2}}'::jsonb),
(123, '{"key_3": {"value_3": 3}}'::jsonb),
(456, '{"key_4": {"value_4": 4}}'::jsonb),
(456, '{"key_1": {"value_3": 4}}'::jsonb)
)
as t (id, values)
)
select id, jsonb_object_agg(t.k, t.v)
from test, jsonb_each(values::jsonb) as t(k,v)
group by id
order by id
;
+---+---------------------------------------------------------------------------+
|id |jsonb_object_agg |
+---+---------------------------------------------------------------------------+
|123|{"key_1": {"value_1": 1}, "key_2": {"value_2": 2}, "key_3": {"value_3": 3}}|
|456|{"key_1": {"value_3": 4}, "key_4": {"value_4": 4}} |
+---+---------------------------------------------------------------------------+
SELECT
proname,
proowner::regrole
FROM pg_proc
WHERE pronamespace::regnamespace::text = 'customer';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment