Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Group by SUM

Tags:

sql

mysql

I have category in a table like

table(cat_name,amount);

How to get the sum of amount each cat_name Grouped by cat_name

like image 593
Sudantha Avatar asked May 24 '11 04:05

Sudantha


People also ask

Can you GROUP BY sum?

SQL SUM() function with group bySUM is used with a GROUP BY clause. The aggregate functions summarize the table data. Once the rows are divided into groups, the aggregate functions are applied in order to return just one value per group.

How do I do an aggregate sum in SQL?

The SQL Server SUM() function is an aggregate function that calculates the sum of all or distinct values in an expression. In this syntax: ALL instructs the SUM() function to return the sum of all values including duplicates. ALL is used by default.

How do I group data in MySQL?

The MySQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.


2 Answers

SELECT cat_name, SUM(amount) AS total_amount
FROM table
GROUP BY cat_name
like image 68
judda Avatar answered Oct 23 '22 21:10

judda


SELECT cat_name, SUM(amount) AS TotalAmount
FROM Table
GROUP BY cat_name
like image 37
Mitch Wheat Avatar answered Oct 23 '22 20:10

Mitch Wheat