MySQL SUM() function with group by
SUM() function with group by
MySQL SUM() function retrieves the sum value of an expression which has undergone a grouping operation by GROUP BY clause.
Example:
Sample table: purchase
Code:
SELECT cate_id,SUM(total_cost)
FROM purchase
GROUP BY cate_id;
Relational Algebra Expression:
Relational Algebra Tree:
Explanation:
The above MySQL statement returns the sum of 'total_cost' from purchase table for each group of category ('cate_id') .
Output:
mysql> SELECT cate_id,SUM(total_cost) -> FROM purchase -> GROUP BY cate_id; +---------+-----------------+ | cate_id | SUM(total_cost) | +---------+-----------------+ | CA001 | 1725.00 | | CA002 | 965.00 | | CA003 | 900.00 | +---------+-----------------+ 3 rows in set (0.00 sec)
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics