JonnoTheDev Posted May 14, 2009 Share Posted May 14, 2009 Having difficulty with this query. What I have is a booking system where a user can be assigned to a booking (many bookings). There is a comission rate for each booking that is to be paid to the user. The administrator can see the amount owed per booking and make payments, however they can pay in parts i.e 50 - 2 installments of 25. What I need is the total amount in comission minus the amount paid for all bookings giving the total owed to the user. tables bookings ======== bookingId userId price rate payMethod user ======== userId name collection ======== id bookingId amount Query: SELECT u.userId, u.name, SUM(b.rate), (SELECT SUM(amount) FROM collection c WHERE b.bookingId=c.bookingId) FROM users u INNER JOIN bookings b ON (u.userId=b.userId) WHERE b.payMethod='cash' GROUP BY u.userId ORDER BY u.name ASC This gives the correct information but I need to minus the results of (SELECT SUM(amount) FROM collection c WHERE b.bookingId=c.bookingId) from SUM(b.rate). A simple minus operator gives me a null result. Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/ Share on other sites More sharing options...
jackpf Posted May 14, 2009 Share Posted May 14, 2009 SELECT u.userId, u.name, SUM(b.rate) - SUM(z.amount) AS `Total` FROM users u INNER JOIN bookings b ON (u.userId=b.userId) INNER JOIN collection z ON b.bookingId=c.bookingId WHERE b.payMethod='cash' GROUP BY u.userId ORDER BY u.name ASC Can't you just do that? Then `Total` will contain the total amount. Possibly... I have no idea if that'll work. Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-833945 Share on other sites More sharing options...
JonnoTheDev Posted May 14, 2009 Author Share Posted May 14, 2009 I cant inner join the collection table as it may contain 0 records for some bookings i.e the sum of b.rate is still owed - no collections have been made by the user Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-833946 Share on other sites More sharing options...
luca200 Posted May 14, 2009 Share Posted May 14, 2009 Try this way SELECT u.userId, u.name, SUM(b.rate) - (ifnull (SELECT SUM(amount) FROM collection c WHERE b.bookingId=c.bookingId), 0 ) FROM users u INNER JOIN bookings b ON (u.userId=b.userId) WHERE b.payMethod='cash' GROUP BY u.userId ORDER BY u.name ASC Not tested... Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-833952 Share on other sites More sharing options...
JonnoTheDev Posted May 14, 2009 Author Share Posted May 14, 2009 no, just gives a query error near the subquery. I may just put the collection total in the same table as the bookings as opposed to logging each individual collection Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-833954 Share on other sites More sharing options...
luca200 Posted May 14, 2009 Share Posted May 14, 2009 no, just gives a query error near the subquery. ...which? Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-833990 Share on other sites More sharing options...
JonnoTheDev Posted May 14, 2009 Author Share Posted May 14, 2009 At this point (ifnull (SELECT SUM(amount) FROM Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-834001 Share on other sites More sharing options...
luca200 Posted May 15, 2009 Share Posted May 15, 2009 You'd better post the EXACT error message Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-834580 Share on other sites More sharing options...
JonnoTheDev Posted May 15, 2009 Author Share Posted May 15, 2009 Thanks, used an alternate solution Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-834607 Share on other sites More sharing options...
fenway Posted May 15, 2009 Share Posted May 15, 2009 Thanks, used an alternate solution That's great! So post it here for everyone else's benefit. Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-834782 Share on other sites More sharing options...
JonnoTheDev Posted May 15, 2009 Author Share Posted May 15, 2009 Oh, its nothing fancy - I mentioned it above I may just put the collection total in the same table as the bookings as opposed to logging each individual collection Quote Link to comment https://forums.phpfreaks.com/topic/158098-solved-math-on-result-of-subquery/#findComment-834867 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.