我希望显示所有待处理发票的详细信息以及显示特定客户待处理发票总数的列,但我无法找到相同的方法。这是我想要做的:
+----------+------------+---------------+---------------+
| 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`
答案 0 :(得分:1)
考虑这个例子......
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 |
+----------+------------+----------------+---------------+
答案 1 :(得分:0)
您可以使用子查询
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