以下是我的表数据
id in out part date
1 20 10 1 2005-02-02
2 10 30 1 2005-02-03
3 20 50 2 2005-02-04
4 20 30 3 2005-02-05
5 100 10 3 2005-02-06
6 20 2 2 2005-02-07
我的期初余额条件是
如果我选择日期范围2到7然后打开余额 (in)形式2到7的总和 - (out)从2到6的总和
答案 0 :(得分:1)
使用SUM
组openss7/src/drivers/clone.c:216:cloneopen(struct inode *inode, struct file *file)
输出:
SELECT part,
SUM(in)-(SELECT SUM(a.out) FROM yourtable a WHERE a.date BETWEEN '2005-02-02' AND '2005-02-06' AND a.part = part) AS OpeningBalance,
SUM(in)-SUM(out) AS ClosingBalance
FROM yourtable
WHERE date BETWEEN '2005-02-02' AND '2005-02-07'
GROUP BY part
SQL FIDDLE:http://sqlfiddle.com/#!3/747c0f/8/0
(在小提琴中,我使用名称ina和outa作为in和out是保留字。