【发布时间】:2017-01-15 12:40:37
【问题描述】:
我有如下数据表:
month marketid totalsold totalshipped lefttoship_thismonth
....
01-01-2015 1 100 50 50
01-01-2015 2 10 3 7
01-01-2015 3 0 0 0
01-02-2015 1 0 50 -50
01-02-2015 2 20 0 20
01-02-2015 3 0 0 0
基本上,此表显示每个市场每月的订单和发货信息。
month 列中的日期01-01-2015 实际上代表Jan 2015(整月)。
我想SUM lefttoship_thismonth 每个市场的每个月与所有前几个月。这是必要的,因为有人可以在一月份下订单,而二月份就提供了。所以我想知道我每月还需要运送多少件物品。
输出应该是:
month marketid totalsold totalshipped totallefttoship TOTALLEFT
01-01-2015 1 100 50 50 50
01-01-2015 2 10 3 7 7
01-01-2015 3 0 0 0 0
01-02-2015 1 0 50 -50 0 /50-50
01-02-2015 2 20 0 20 27 /7+20
01-02-2015 3 0 0 0 0 / 0+0
我该怎么做?
我不知道如何以这种方式求和,month 列很难使用。
【问题讨论】:
标签: sql postgresql