I was trying to sum up all the values from an array in PostgreSQL, tried different way but always getting the issue function sum(numeric[]) does not exist. Even I have tried with casting, but still that shows the same issue. Am I missing anything here?.
select array_to_json(array_agg(items)) FROM (SELECT SUM(p.total_price) FROM (SELECT
ARRAY_AGG(cart_items.unit_price) FILTER (where cart_items.type = 2) AS total_price
from cart inner join cart_items on cart_items.cart_id = cart.id where cart.id =
40868884) AS p) AS items;
Result is:
array_to_json
-------------------------------------------------------------------------------------------------
[{"total_price":[1.867,1.867,1.867,1.867,1.867,1.21,1.867,1.867,1.56,1.867,1.867,1.867,44]}]
Basically, I want to sum up these values of the column prices after taking it from the database.
Fiddle