六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 161|回复: 0

PostgreSQL 实现累加

[复制链接]

升级  1.67%

57

主题

57

主题

57

主题

举人

Rank: 3Rank: 3

积分
205
 楼主| 发表于 2013-1-30 01:30:48 | 显示全部楼层 |阅读模式
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为累加的字段
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表