【发布时间】:2021-08-29 11:47:21
【问题描述】:
我试图总结 PostgreSQL 中数组中的所有值,尝试了不同的方法,但总是遇到问题 function sum(numeric[]) does not exist。即使我尝试过铸造,但仍然显示出同样的问题。我在这里有什么遗漏吗?
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;
结果是:
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]}]
基本上,我想在从数据库中取出列价格后将这些值汇总。
小提琴
【问题讨论】:
-
你能设置一个 dbfiddle 吗?您的查询没有
sum(),所以这似乎是一个奇怪的错误。 -
其实我已经写了正确的语句,现在我已经更新了 SUM,@GordonLinoff
-
我添加了小提琴@GordonLinoff