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
my guess is that the payment is $10 and there are two items in the order (orderitems table). if that's the case, try using a GROUP BY on the orders/customers/orderstatus fields.
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:
, <earlier columns>
, ( select SUM(payments.amount)
from payments
where payments.orderID = orders.id
) AS totalPayments
, <later columns>
This would ensure the payments with multiple orderitems are not summed multiple times.