Ahmed
Ahmed

Reputation: 1533

MySQL sum and grouping extra row

If I run the following MySQL query:

SELECT username, sum(value) as value 
FROM ztemp 
GROUP BY username

I get results with each username and the sum value. However, if do the following query:

SELECT username, sum(value) as value 
FROM ztemp 

Then it gives me the overal sum instead of user by user.

Rather than running two SQL queries to display the result. Is there a way to get both in one SQL query? Or is it better to run the two queries?

Upvotes: 1

Views: 267

Answers (1)

O. Jones
O. Jones

Reputation: 108736

Yes, there's a way to do this ... using the ROLLUP modifier.

GROUP BY username WITH ROLLUP

gives the extra row.

Nice, huh?

Upvotes: 1

Related Questions