我想从你们那里得到一点确认:
我有一个视图,其中包含(除其他外)列 time_in、time_out、Company、weight,它显示了公司何时将一些重量放入仓库以及何时(如果有的话)将其取出。所以它看起来像这样:
time_in time_out company weight
1-07-2013 3-07-2013 A 10
2-07-2013 NULL A 15
2-07-2013 4-07-2013 B 5
3-07-2013 NULL B 1
ETC
现在我想为每家公司的每一天做一个平衡,所以它会是这样的:
Date Company Balance
1-07-2013 A 10
1-07-2013 B 0
2-07-2013 A 25
2-07-2013 B 5
3-07-2013 A 15
3-07-2013 B 6
4-07-2013 A 15
4-07-2013 B 1
ETC
虽然我知道如何计算基本的运行总计,即
SELECT a.time_IN, a.weight, (SELECT sum (b.weight) from DEPOT b
where b.time_in <= a.time_in) as total_weight from DEPOT a
我猜想平衡会是这样的
SELECT a.time_IN, a.weight, ((SELECT sum (b.weight)
from DEPOT b where b.time_in <= a.time_in)
-(SELECT sum (c.weight) from DEPOT c
where a.time_out >= c.time_out) as balance from DEPOT a
我在这里朝着正确的方向前进吗?另外,我是否必须简单地添加 GROUP BY 公司才能按公司获取信息?