Skip to content
Advertisement

My SUM with cases seems to be repeating twice

I have some camp management software that registers users for a camp. I am trying to get how much a user owes on their account based on how much a camp costs and whether they are using the bus, and whether or not they sign up for the horse option. (These all cost extra).

I originally was grouping by registration_ids which a camper can have multiple of if they sign up for a camp. But when I put this in I get this:

https://imgur.com/i63Bnsu

This is my sql:

This seems to be affected by how many payments they have made in their account. It multiplies the amount they owe times how many individual payments were made toward that camp. I can’t figure out how to stop this.

For instance in picture above^

We have camper_id #4 and they owe 678. I expect camper_id #4 to owe 339. They have made 2 payments on their account in srbc_payments.

Haven’t been using sql for that long, so any suggestions for a better way I am open too!

Advertisement

Answer

This is what I ended up getting to work how I wanted it too:

I kind of understand what I did here. I ended up trying different things from this answer: My SUM with cases seems to be repeating twice

Thanks for the helpful comments from @nick and @a_horse_with_no_name

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement