I have two tables I wish to join on lets say table a and table b. Table b has many rows to table a's, table b contains prices (effectively a shopping basket). So what I want is all records from table a and the sum of the price from table b. I have tried
select a.*, sum(b.ach_sell) from bookings a
left join pricing_line b on b.bookings = a.id
However this obviously doesn't do as I wish, it ends up with the total sum of all ach_sell (so one record is returned). Would someone kindly offer me a solution which would help? right now I am doing it programatically and I am pretty sure it could be done in SQL?
Your direction is right, just add a group by clause to separate the a.id's, something like this:
select a.id, sum(b.ach_sell)
from bookings a
left join pricing_line b
on b.bookings = a.id
group by a.id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With