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
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.
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.
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.
SELECT cat_name, SUM(amount) AS total_amount
FROM table
GROUP BY cat_name
SELECT cat_name, SUM(amount) AS TotalAmount
FROM Table
GROUP BY cat_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With