My SQL query:
select
*,
(select sum(Amount) as TotalAmount
from Payment
where Customer.Id = Payment.CustomerId)
from Customer
The result:
Id - CustomerName - CustomerPhoneNumber - TotalAmount
1 - TestUser - TestLastName - 3000
2 - TestUser2 - TestLastName2 - 2500
This is working as I expected. But are there any better ways to get same result ?
If it’s MySQL, then you can do this…
In other varieties of SQL, you need to GROUP BY everything that is selected but not an aggregate.