Re: sum in mysql
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.
Subject
Written By
Posted
Re: sum in mysql
April 04, 2022 07:21AM
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.