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:

SELECT      srbc_campers.camper_id,
/*Calculate how much the user owes*/
    SUM(
    srbc_camps.cost + (CASE WHEN srbc_registration.horse_opt = 1 THEN srbc_camps.horse_opt_cost
    ELSE 0
    END)
     +
    (CASE WHEN srbc_registration.busride = 'to' THEN 35
    WHEN srbc_registration.busride = 'from' THEN 35
    WHEN srbc_registration.busride = 'both' THEN 60
    ELSE 0
    END) 
    - IF(srbc_registration.discount IS NULL,0,srbc_registration.discount)
    - IF(srbc_registration.scholarship_amt IS NULL,0,srbc_registration.scholarship_amt)     
    ) AS owe


    FROM (
    srbc_registration INNER JOIN srbc_camps ON srbc_registration.camp_id=srbc_camps.camp_id)
    INNER JOIN srbc_payments ON srbc_registration.registration_id = srbc_payments.registration_id)
          INNER JOIN srbc_campers ON srbc_campers.camper_id=srbc_registration.camper_id)
    WHERE NOT srbc_payments.payment_type='Store'
    GROUP BY srbc_campers.camper_id

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:

SELECT  owedTble.registration_id,owe
FROM (SELECT registration_id,
                        SUM(
                                srbc_camps.cost + (CASE WHEN srbc_registration.horse_opt = 1 THEN srbc_camps.horse_opt_cost
                                ELSE 0
                                END)
                                 +
                                (CASE WHEN srbc_registration.busride = 'to' THEN 35
                                WHEN srbc_registration.busride = 'from' THEN 35
                                WHEN srbc_registration.busride = 'both' THEN 60
                                ELSE 0
                                END) 
                                - IF(srbc_registration.discount IS NULL,0,srbc_registration.discount)
                                - IF(srbc_registration.scholarship_amt IS NULL,0,srbc_registration.scholarship_amt)     
                                ) AS owe

            FROM srbc_camps INNER JOIN srbc_registration ON srbc_camps.camp_id=srbc_registration.camp_id
      GROUP BY srbc_registration.registration_id
) as owedTble

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