【问题标题】:What is the solution for this?解决方案是什么?
【发布时间】:2018-04-02 06:57:29
【问题描述】:
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) ;

我需要得到如上所示的期末余额。

【问题讨论】:

  • 请解释一下。并找到一个没有用的标题。阅读How to Askhelp centerminimal reproducible example并采取行动。
  • 首先修复你的连接 - 语法是连接表 ...其次,我们无法知道表是聚合还是查询需要聚合函数 - 将示例数据和预期结果作为文本发布或到 sqlfiddle。第三,即使没有销售或没有问题,我们也无法告诉您想要从 opening_balances 获得的一切。
  • @P.Salmon MySQL 允许(内部)连接而不打开,这意味着交叉连接。 MASHFERDISTA 阅读有关使用与正在连接的表对相关的条件的良好风格。你也误用了=,你不能把它们排成一行来表示不同等式的结合。
  • @philipxy 我知道 - 在我看来很糟糕
  • @P.Salmon 我不介意,因为 inner join 只是 cross join, 只是 cross join,绑定更松,on 只是 where,绑定更紧捆绑。 on 只有outer joins 才需要

标签: mysql sql select balance


【解决方案1】:

根据你的数据

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)

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 2019-03-09
    • 1970-01-01
    • 2013-08-02
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多