MySQL Forums
Forum List  »  Newbie

Re: sum in mysql
Posted by: Phillip Ward
Date: April 04, 2022 07:21AM

If you're using SUM to generate multiple rows, then you must have a "group by" clause that generates each row.
To get the "grand total", remove the "group by" clause to get a single-row total.

select * from table1 ; 

+----+-----+-----+
| id | key | qty |
+----+-----+-----+
| 77 | a   |   3 | 
| 88 | a   |   6 | 
| 99 | b   |  12 |
+----+-----+-----+

select 
  key
, sum( qty ) qty 
from table1
group by key 

+-----+-----+
| key | qty | 
+-----+-----+
| a   |   9 |
| b   |  12 |
+-----+-----+

select 
  sum( qty ) qty 
from table1

+-----+
| qty | 
+-----+
|  21 |
+-----+

Regards, Phill W.

Options: ReplyQuote


Subject
Written By
Posted
March 31, 2022 08:20AM
March 31, 2022 10:02AM
March 31, 2022 10:08AM
Re: sum in mysql
April 04, 2022 07:21AM
April 01, 2022 09:25AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.