Jump to content

[SOLVED] SUM of Returned Column Values


bubbadawg

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/152276-solved-sum-of-returned-column-values/
Share on other sites

 

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.