select opening_balances.branch_ID, opening_balances.comm_ID,
(ifnull(comm_issue.comm_issue_quantity,0) - ifnull(comm_sales.comm_sale_quantity,0)) AS net,
(opening_balances.comm_quantity + net) AS Closing_Balance
FROM opening_balances
JOIN comm_sales JOIN comm_issue
ON opening_balances.branch_ID = ifnull(comm_sales.branch_ID,0) = ifnull(comm_issue.branch_ID,0) = opening_balances.branch_ID
&& opening_balances.comm_ID = ifnull(comm_sales.comm_ID,0) = opening_balances.comm_ID = ifnull(comm_issue.comm_ID,0) ;
我需要获得如上所示的期末余额。
答案 0 :(得分:0)
鉴于您的数据
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');
你的查询似乎试图通过branch和comm_id来获得一些总数。如果是这种情况,我希望看到一些聚合函数(sum)和group by。也许就像这样
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)