ded Posted July 7, 2011 Share Posted July 7, 2011 I need some help creating a query My database has the following fields: firstname lastname games points average event year Each player can have 1 - 8 records at this point I want to be able to GROUP BY firstname and lastname combined. I want to sort by the combined average. So basically I will need to sum games and points and then divide them to create the overall average. Link to comment https://forums.phpfreaks.com/topic/241337-sum-and-group-by-help/ Share on other sites More sharing options...
ded Posted July 7, 2011 Author Share Posted July 7, 2011 Examples records: First Last games points average event year John Doe 17 904 53.18 ABDA Nationals 2010 John Doe 21 1132 53.90 PA States 2010 John Doe 8 414 51.75 PA States 2009 I put in this code SELECT * , SUM(`games`) , SUM(`points`) , SUM(`average`) FROM `averages` GROUP BY `lastname` ,`firstname` ORDER BY `lastname` ,`firstname` LIMIT 0 , 9999 This shows the following First Last SUM(`games`) SUM(`points`) SUM(`average`) John Doe 46 2450 158.83 For the SUM(`average`), I need it to show 53.26 which is 2450/46. I need it to show the following: First Last SUM(`games`) SUM(`points`) SUM(`average`) John Doe 46 2450 53.26 Link to comment https://forums.phpfreaks.com/topic/241337-sum-and-group-by-help/#findComment-1239683 Share on other sites More sharing options...
ded Posted July 7, 2011 Author Share Posted July 7, 2011 Never mind....got it SELECT * , (SUM(`points`)/SUM(`games`)) FROM `averages` WHERE `lastname` = 'Young' GROUP BY `lastname` ,`firstname` ORDER BY `lastname` ,`firstname` LIMIT 0 , 9999 Link to comment https://forums.phpfreaks.com/topic/241337-sum-and-group-by-help/#findComment-1239692 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.