I am getting some basic invoice information in a SQL query and figuring the Order Total and Payment Totals in the same query. Here is what I have thus far:
S
If you run the query without a group by, you'll see that some payments have multiple rows. That's because you're also joining on order items. The result set will contain a row for each combination of orderitem and payment.
One solution would be to change the sum to:
,
, ( select SUM(payments.amount)
from payments
where payments.orderID = orders.id
) AS totalPayments
,
This would ensure the payments with multiple orderitems are not summed multiple times.