Sample Header Ad - 728x90

User-defined aggregate function to sum jsonb values

0 votes
1 answer
135 views
I have agg function that sums decimals. Business logic omitted for brevity
CREATE OR REPLACE FUNCTION calculate_snapshot_internal_decimal(_this_value DECIMAL,
                                                               _next_value DECIMAL
                                                               OUT _code DECIMAL)
    LANGUAGE plpgsql AS
$func$
BEGIN

    _code = _this_value + _next_value;

END
$func$;
Now I need the same for JSONB where key is string and value is always BigDecimal _this_value:
{"weight": "50", "height":"170", "size": "2"}
_next_value
{"weight": "40", "height":"20"}
aggregated result:
{"weight": "90", "height":"190", "size": "2"}
What could I try?
Asked by Capacytron (137 rep)
Oct 3, 2023, 01:27 PM
Last activity: Nov 29, 2023, 05:22 AM