在我的表中,我有5列5行,现在我只需要添加3行的AmountPaid
列,并希望存储在另一个表中。这3行包含唯一代码并记录到这个唯一代码我希望将这个总数加入新表。
我尝试了这段代码,但没有按照我的方式工作。
SELECT InvoiceNo, InvoiceDate, SalesPerson(SELECT SalesPerson FROM
SalesDepartment WHERE SalesDepartment.SalesPersonId=Invoice.SalesPerson)
AS SalesPerson, Customer, OrderNumber, GrandTotal,
AmountPaid(SELECT AmountPaid FROM Payment WHERE
Payment.InvoiceNo=Invoice.InvoiceNo) AS AmountPaid
FROM Invoice;
如果我删除最后一个AmountPaid列,则此代码正常工作。但是我需要添加一个表的付费金额并将其存储在另一个表列中。如何完成这项任务?
答案 0 :(得分:1)
希望这有帮助。
子查询:
SELECT InvoiceNo,
InvoiceDate,
(SELECT SalesPerson FROM SalesDepartment WHERE SalesDepartment.SalesPersonId=Invoice.SalesPerson) AS SalesPerson,
Customer,
OrderNumber,
GrandTotal,
(SELECT SUM(AmountPaid) FROM Payment WHERE Payment.InvoiceNo=Invoice.InvoiceNo) AS totalAmountPaid
FROM Invoice;
Equijion:
SELECT InvoiceNo,
InvoiceDate,
SalesPerson,
Customer,
OrderNumber,
GrandTotal,
SUM(AmountPaid) AS totalAmountPaid
FROM Invoice
INNER JOIN SalesDepartment ON SalesDepartment.SalesPersonId=Invoice.SalesPerson
INNER JOIN Payment ON Payment.InvoiceNo=Invoice.InvoiceNo
GROUP BY InvoiceNo