How to sort objects in an array inside a json or jsonb value by a property of the objects?
问题 I have this pl/pgsql function to aggregate rows from two tables in a jsonb value ( data_table_1 and data_table_2 ). fk_id is a common foreign key id in both tables: DECLARE v_my_variable_1 jsonb; v_my_variable_2 jsonb; v_combined jsonb; BEGIN SELECT json_agg( data_table_1 ) INTO v_my_variable FROM data_table_1 WHERE fk_id = v_id; SELECT json_agg( data_table_2 ) into v_my_variable_2 FROM data_table_2 WHERE fk_id = v_id; SELECT v_my_variable || v_my_variable_2 into v_combined; Now I want to