postgresql - 如何对一行 PostgreSQL 中的值求和?
问题描述
我有一个问题,将所有值汇总在一行中。表是这样的:
如何像这样对所有值求和?
公式是这样的:
column 2019 = 2019,
column 2020 = 2020 + column 2019,
column 2021 = 2021 + column 2020,
column 2022 = 2022 + column 2021,
column total = same as the previous column
我的查询:
SELECT 3 AS id,
'Equity / Modal'::text AS title,
COALESCE(sum(a."2019") OVER (ORDER BY a.transaction) , 0::numeric) AS "2019",
COALESCE(sum(a."2020" + "2019") OVER (ORDER BY a.transaction), 0::numeric) AS "2020",
COALESCE(sum(a."2021" + "2020") OVER (ORDER BY a.transaction), 0::numeric) AS "2021",
COALESCE(sum(a."2022" + "2021") OVER (ORDER BY a.transaction), 0::numeric) AS "2022",
COALESCE(sum("2022") OVER (ORDER BY a.transaction), 0::numeric) AS total
FROM vw_blm_dashboard_iurantransaksi_anggota a
WHERE a.transaction = 'SUBTOTAL';
解决方案
推荐阅读
- flutter - 在flutter中使用setState和streamBuilder的正确方法或区别
- amazon-web-services - 为 AWS Lambda@Edge 选择边缘站点
- javascript - 将整数从 JSP 发送到 Servlet
- javascript - jQuery在点击上添加越来越多的项目
- c# - 事件到命令行为不执行命令
- python - python中的时间序列分割
- swift - 调用时未在 UIImageView 中获取二进制数据
- visual-studio-code - VS Code 在 ech CSS 保存时要求输入密码
- jquery - 如何在第二次点击时反转css动画
- highcharts - Highcharts 添加按钮修复图表