Hi there,
I have Orders and Payments. Sometimes the Orders do not have Payments. So, I
decided to use LEFT JOIN.
Furthermore, some Orders have 1 related Payment, and some Orders have 2 or
even more related Payments.
Now, I want to combine the sum of all Payments and the last Payment in one
row.
select Orders.SeqNr,
sum(B1.Amount) Total_paid,
max(B2.SeqNr) Last_record,
B2.Amount Last_amount
from Orders
left join Payments B1
on B1.FK_Orders = Orders.SeqNr
left join Payments B2
on B2.FK_Orders = Orders.SeqNr
group by Orders.SeqNr
If there is only 1 payment per order, then I will see perfect results.
However, if there is more than 1 payment per order, then the results are
wrong...
The column Last_amount does not contain the amount which belongs to the
column Last_record.
I have tried everything.........
Please help me out here.
MySQL version = 4.0.
Regards,
Stefan.