Select sum of an array column in PostgreSQL

风格不统一 提交于 2019-12-03 01:33:29
Dmitry Seleznev
SELECT id, (SELECT SUM(s) FROM UNNEST(monthly_usage) s) as total_usage from users;

This generalization and reformatting Dmitry's answer helps me understand how it works:

SELECT
  sum(a) AS total
FROM
  (
    SELECT
      unnest(array [1,2,3]) AS a
  ) AS b

Result:

total
 6
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!