根据你的数据
drop table if exists opening_balances,comm_sales,comm_issue;
create table Opening_balances
(`Date` date, branch_ID int, comm_ID int, comm_quantity int);
insert into opening_balances values
('2017-11-01',1,1,5), ('2017-11-01',1,2,6), ('2017-11-01',2,1,7), ('2017-11-01',2,2,8),
('2017-11-01',3,2,10);
create table comm_sales
(branch_ID int, comm_ID int, comm_sale_quantity int, `Date` datetime);
insert into comm_sales values
(1,1,3,'2017-10-19 11:03:15'), (1,2,3,'2017-10-19 11:03:30'), (2,1,3,'2017-10-19 11:03:37'), (2,2,4,'2017-10-19 11:03:42');
create table comm_issue
(branch_ID int, comm_ID int, comm_issue_quantity int, `Date` datetime);
insert into comm_issue values
(1,1,3,'2017-10-21 04:37:05');
您的查询似乎试图按分支和 comm_id 获取一些总计。如果是这种情况,我希望看到一些聚合函数(总和)和分组依据。可能是这样的
select ob.branch_id, ob.comm_id,
coalesce(sum(ob.comm_quantity),0) OpeningQTY,
Coalesce(sum(ci.comm_issue_quantity),0) IssueQty,
coalesce(sum(cs.comm_sale_quantity),0) SaleQty,
coalesce(sum(ci.comm_issue_quantity),0) - coalesce(sum(cs.comm_sale_quantity),0) net,
coalesce(sum(ob.comm_quantity),0) +
coalesce(sum(ci.comm_issue_quantity),0) -
coalesce(sum(cs.comm_sale_quantity),0) ClosingQty
from opening_balances ob
left join comm_sales cs on cs.branch_id = ob.branch_id and cs.comm_id = ob.comm_id
left join comm_issue ci on ci.branch_id = ob.branch_id and ci.comm_id = ob.comm_id
group by ob.branch_id , ob.comm_id
+-----------+---------+------------+----------+---------+------+------------+
| branch_id | comm_id | OpeningQTY | IssueQty | SaleQty | net | ClosingQty |
+-----------+---------+------------+----------+---------+------+------------+
| 1 | 1 | 5 | 3 | 3 | 0 | 5 |
| 1 | 2 | 6 | 0 | 3 | -3 | 3 |
| 2 | 1 | 7 | 0 | 3 | -3 | 4 |
| 2 | 2 | 8 | 0 | 4 | -4 | 4 |
| 3 | 2 | 10 | 0 | 0 | 0 | 10 |
+-----------+---------+------------+----------+---------+------+------------+
5 rows in set (0.02 sec)
如果您想获得小计,您可以在查询末尾添加汇总
select ob.branch_id, ob.comm_id,
coalesce(sum(ob.comm_quantity),0) OpeningQTY,
Coalesce(sum(ci.comm_issue_quantity),0) IssueQty,
coalesce(sum(cs.comm_sale_quantity),0) SaleQty,
coalesce(sum(ci.comm_issue_quantity),0) - coalesce(sum(cs.comm_sale_quantity),0) net,
coalesce(sum(ob.comm_quantity),0) +
coalesce(sum(ci.comm_issue_quantity),0) -
coalesce(sum(cs.comm_sale_quantity),0) ClosingQty
from opening_balances ob
left join comm_sales cs on cs.branch_id = ob.branch_id and cs.comm_id = ob.comm_id
left join comm_issue ci on ci.branch_id = ob.branch_id and ci.comm_id = ob.comm_id
group by ob.branch_id , ob.comm_id with rollup;
+-----------+---------+------------+----------+---------+------+------------+
| branch_id | comm_id | OpeningQTY | IssueQty | SaleQty | net | ClosingQty |
+-----------+---------+------------+----------+---------+------+------------+
| 1 | 1 | 5 | 3 | 3 | 0 | 5 |
| 1 | 2 | 6 | 0 | 3 | -3 | 3 |
| 1 | NULL | 11 | 3 | 6 | -3 | 8 |
| 2 | 1 | 7 | 0 | 3 | -3 | 4 |
| 2 | 2 | 8 | 0 | 4 | -4 | 4 |
| 2 | NULL | 15 | 0 | 7 | -7 | 8 |
| 3 | 2 | 10 | 0 | 0 | 0 | 10 |
| 3 | NULL | 10 | 0 | 0 | 0 | 10 |
| NULL | NULL | 36 | 3 | 13 | -10 | 26 |
+-----------+---------+------------+----------+---------+------+------------+
9 rows in set (0.00 sec)