Jump to content

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.