Adamhumbug Posted August 15, 2023 Share Posted August 15, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317190-total-count-as-well-as-total-per-client/ Share on other sites More sharing options...
Adamhumbug Posted August 15, 2023 Author Share Posted August 15, 2023 Actually my question is the other way around, i have the total amount just not the amount per client - that might help with my googling. Quote Link to comment https://forums.phpfreaks.com/topic/317190-total-count-as-well-as-total-per-client/#findComment-1611170 Share on other sites More sharing options...
Solution mac_gyver Posted August 15, 2023 Solution Share Posted August 15, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317190-total-count-as-well-as-total-per-client/#findComment-1611171 Share on other sites More sharing options...
Adamhumbug Posted August 15, 2023 Author Share Posted August 15, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/317190-total-count-as-well-as-total-per-client/#findComment-1611173 Share on other sites More sharing options...
mac_gyver Posted August 15, 2023 Share Posted August 15, 2023 while there probably is a way of doing this in a query, with a sub-query/self-join, i would just perform the calculation when you produce the output. Quote Link to comment https://forums.phpfreaks.com/topic/317190-total-count-as-well-as-total-per-client/#findComment-1611174 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.