PostgreSQL 实现累加

exchange=# select invoice_id, client_id , total_amount,invoice_end   from  invoice   order by invoice_id ;
invoice_id | client_id | total_amount | invoice_end
------------+-----------+--------------+------------------------
31 | 1 | 14.06202 | 2010-12-20 00:00:00+00
71 | 4 | -45.25617 | 2010-12-20 00:00:00+00
75 | 3 | 2.52500 | 2010-12-17 00:00:00+00
76 | 3 | 9.36850 | 2010-12-18 00:00:00+00
77 | 3 | 34.40367 | 2010-12-19 00:00:00+00
78 | 3 | 0.00000 | 2010-12-20 00:00:00+00
(6 rows)

exchange=#


现在将 total_amount累加
exchange=# select                                                                                         
invoice.invoice_id, invoice.client_id ,invoice. total_amount,invoice.invoice_end,
(select sum(total_amount) as past_due from invoice as inner_invoice where client_id = invoice.client_id and inner_invoice.invoice_end <= invoice.invoice_end) as grand_total
from invoice order by invoice.invoice_id;
invoice_id | client_id | total_amount | invoice_end | grand_total
------------+-----------+--------------+------------------------+-------------
31 | 1 | 14.06202 | 2010-12-20 00:00:00+00 | 14.06202
71 | 4 | -45.25617 | 2010-12-20 00:00:00+00 | -45.25617
75 | 3 | 2.52500 | 2010-12-17 00:00:00+00 | 2.52500
76 | 3 | 9.36850 | 2010-12-18 00:00:00+00 | 11.89350
77 | 3 | 34.40367 | 2010-12-19 00:00:00+00 | 46.29717
78 | 3 | 0.00000 | 2010-12-20 00:00:00+00 | 46.29717
(6 rows)

exchange=#

grand_total为累加的字段
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值