Jump to content

Total count as well as total per client


Go to solution Solved by mac_gyver,

Recommended Posts

Hi All,

I fear this may be very simple but i have been struggling a little with it - brain fog.

I am running the following query:

SELECT client.id, company_name, country, DATE_FORMAT(date_created, '%d %M %Y') as date_created, sum(total_value) as tval
            from client
            inner join quote on client.id = quote.client_id

but i also want to do a count of everything that is in the total_value column so i end up with a number per client and a total of everything.

Any pointers appreciated.

Link to comment
https://forums.phpfreaks.com/topic/317190-total-count-as-well-as-total-per-client/
Share on other sites

  • Solution

you would add GROUP BY client.id to the query to get a SUM() per client. in fact, if your database was set to STRICT mode, you would be getting an  error with the current query about using an aggerate function without a GROUP BY term.

then, if you want both the SUM() per client and the grand total, you can add WITH ROLLUP to the end of the GROUP BY ... term.

2 hours ago, mac_gyver said:

you would add GROUP BY client.id to the query to get a SUM() per client. in fact, if your database was set to STRICT mode, you would be getting an  error with the current query about using an aggerate function without a GROUP BY term.

then, if you want both the SUM() per client and the grand total, you can add WITH ROLLUP to the end of the GROUP BY ... term.

Amazing, that is absolutely what i was looking for but does lead me on to another question.

As i am wanting to show on each row what percentage of the overall total each client makes up i would need to do something different with the rollup as currently it adds another row to the table.

Any suggestions on how to deal with that?

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.