bubbadawg Posted April 2, 2009 Share Posted April 2, 2009 I have the following query: SELECT DISTINCT project, SUM(hours) as Hours FROM `projects_tbl` GROUP BY project This returns data similar to the following: project Hours dev#1 5 dev#2 15 dev#3 12 dev#4 8 dev#5 10 How can I get the sum of all the Hours (50) from all the projects in the returned query? Thanks for any and all replies. Quote Link to comment https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/ Share on other sites More sharing options...
Zane Posted April 2, 2009 Share Posted April 2, 2009 going out on a limb here but maybe SELECT SUM(hours) as Hours FROM `projects_tbl` GROUP BY project Quote Link to comment https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/#findComment-799673 Share on other sites More sharing options...
fenway Posted April 2, 2009 Share Posted April 2, 2009 Add "WITH ROLLUP" to your group by. Aside: Drop the DISTINCT. Quote Link to comment https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/#findComment-799680 Share on other sites More sharing options...
bubbadawg Posted April 2, 2009 Author Share Posted April 2, 2009 Add "WITH ROLLUP" to your group by. Aside: Drop the DISTINCT. Thanks, that did it! The resulting total is returned with a NULL value for the row name. Is it possible for me to set a name for the row? Quote Link to comment https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/#findComment-799685 Share on other sites More sharing options...
fenway Posted April 3, 2009 Share Posted April 3, 2009 Thanks, that did it! The resulting total is returned with a NULL value for the row name. Is it possible for me to set a name for the row? You should be able to handle this trivially in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/#findComment-800278 Share on other sites More sharing options...
bubbadawg Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks, that did it! The resulting total is returned with a NULL value for the row name. Is it possible for me to set a name for the row? For anyone who is interested, using COALESCE will supply the label, in this case 'Total' for the last row. Here's the complete query: SELECT COALESCE(project,'Total')project, SUM(hours) as Hours FROM `projects_tbl` GROUP BY project WITH ROLLUP; Quote Link to comment https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/#findComment-802687 Share on other sites More sharing options...
fenway Posted April 7, 2009 Share Posted April 7, 2009 So will IFNULL(). Quote Link to comment https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/#findComment-803949 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.