我在一个SQL查询中获取一些基本的发票信息,并在同一个查询中计算订单总额和付款总额。以下是我目前为止的情况:
SELECT
orders.billerID,
orders.invoiceDate,
orders.txnID,
orders.bName,
orders.bStreet1,
orders.bStreet2,
orders.bCity,
orders.bState,
orders.bZip,
orders.bCountry,
orders.sName,
orders.sStreet1,
orders.sStreet2,
orders.sCity,
orders.sState,
orders.sZip,
orders.sCountry,
orders.paymentType,
orders.invoiceNotes,
orders.pFee,
orders.shipping,
orders.tax,
orders.reasonCode,
orders.txnType,
orders.customerID,
customers.firstName AS firstName,
customers.lastName AS lastName,
customers.businessName AS businessName,
orderStatus.statusName AS orderStatus,
SUM((orderItems.itemPrice * orderItems.itemQuantity))
+ orders.shipping + orders.tax AS orderTotal,
SUM(payments.amount) AS totalPayments <-- this sum
FROM
orders
LEFT JOIN customers ON orders.customerID = customers.id
LEFT JOIN orderStatus ON orders.orderStatus = orderStatus.id
LEFT JOIN payments ON payments.orderID = orders.id <-- this join
LEFT JOIN orderItems ON orderItems.orderID = orders.id
除了totalpayments列之外,查询中的所有内容都非常出色。数据库中有一个值为(10.00)的付款。查询提供的值为20.00(正好是双倍)。我的理论是,出于某种原因,查询是将“付款金额”列“求和”两次。有人能帮我解释一下吗?
谢谢你的帮助!