Mysql 显示每一行时,基于客户的未决金额总和

Mysql 显示每一行时,基于客户的未决金额总和,mysql,Mysql,我希望显示所有待决发票的详细信息以及显示特定客户待决发票总数的列,但我无法找到相同的方法。以下是我试图做的: +----------+------------+---------------+---------------+ | Customer | Invoice No | Peding Amount | Total Pending | +----------+------------+---------------+---------------+ | A |

我希望显示所有待决发票的详细信息以及显示特定客户待决发票总数的列,但我无法找到相同的方法。以下是我试图做的:

+----------+------------+---------------+---------------+
| Customer | Invoice No | Peding Amount | Total Pending |
+----------+------------+---------------+---------------+
| A        |          1 |            10 |          1110 |
| B        |          2 |           100 |           110 |
| C        |          3 |          1000 |          3020 |
| A        |          4 |          1000 |          1110 |
| A        |          5 |           100 |          1110 |
| B        |          6 |            10 |           110 |
| C        |          7 |          2000 |          3020 |
| C        |          8 |            20 |          3020 |
+----------+------------+---------------+---------------+
现在我想通知您,该表只有前3列,但我需要添加第4列,但我无法找到基于客户的求和方法

这是我试图使用的代码,但我得到了某种语法

select 
 `tabSales Invoice`.`posting_date` as "Invoice Date:Date:80",
 `tabSales Invoice`.`due_date` as "Due Date:Date:80",
 `tabSales Invoice`.`name` as "Invoice No:Link/Sales Invoice:120",
 `tabSales Invoice`.`customer` as "Customer:Link/Customer:180",
 `tabSales Invoice`.`grand_total` as "Total:Currency:140",
 `tabSales Invoice`.`outstanding_amount` as "Pending:Currency:140",
 datediff(curdate(),`tabSales Invoice`.`posting_date`) as "Over By Invoice Date:Float:80",
 datediff(curdate(),`tabSales Invoice`.`due_date`) as "Over By Due Date:Float:80",
 `tabSales Invoice`.`debit_to` as "Customer Account:Link/Account:200"
from
 `tabSales Invoice`
where
 `tabSales Invoice`.`docstatus` = 1
 and `tabSales Invoice`.`outstanding_amount` > 0.005


Inner join(
    Select 
    `tabSales Invoice`.`customer`,
    SUM(`tabSales Invoice`.`outstanding_amount`) AS "Total Pending::180"
    from
     `tabSales Invoice`
     Group By
      `tabSales Invoice`.`customer`) 
     `tabSales Invoice` ON `tabSales Invoice`.`customer`
您可以使用子查询

SELECT customer, invoiceno, pendingamount, pending
FROM mytable 
    INNER JOIN (
    SELECT customer, SUM(pendingamount) AS pending
    FROM mytable
    GROUP BY customer) a on mytable.customer ON a.customer

考虑一下这个例子

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table
 (Customer CHAR(1) NOT NULL
 ,Invoice_No INT NOT NULL
 ,Pending_Amount INT NOT NULL
 ,PRIMARY KEY(Customer,Invoice_No)
 );

 INSERT INTO my_table VALUES
 ('A',1,10),
 ('B',2,100),
 ('C',3,1000,
 ('A',4,1000),
 ('A',5,100),
 ('B',6,10),
 ('C',7,2000),
 ('C',8,20);

 SELECT x.*
      , SUM(y.Pending_Amount) Total_Pending 
   FROM my_table x 
   JOIN my_table y 
     ON y.customer = x.customer 
  GROUP 
     BY x.customer
      , x.invoice_no;
 +----------+------------+----------------+---------------+
 | Customer | Invoice_No | Pending_Amount | Total_Pending |
 +----------+------------+----------------+---------------+
 | A        |          1 |             10 |          1110 |
 | A        |          4 |           1000 |          1110 |
 | A        |          5 |            100 |          1110 |
 | B        |          2 |            100 |           110 |
 | B        |          6 |             10 |           110 |
 | C        |          3 |           1000 |          3020 |
 | C        |          7 |           2000 |          3020 |
 | C        |          8 |             20 |          3020 |
 +----------+------------+----------------+---------------+

这真的是您想要的结果集吗(在结果集中的多个记录中重复相同的总数不是很“相关”的)?或者您只需要了解每个客户及其各自的总数?或者可能是一个正在运行的总数?所以基本上是total Pending=客户分组的SUM(Pending Amount),对吗?您可以使用一个临时表来保存客户发票号和PendingAmt,然后计算并更新总待定列,如Set TotalPending=[select SUM(PendingAmt),Customer from temp group by Customer],其中Customer=cust.Customer[cust]