How can find sum of a column after group by result in mysql?

Member

by deron , in category: MySQL , 7 months ago

How can find sum of a column after group by result in mysql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 7 months ago

@deron 

To find the sum of a column after using the GROUP BY clause in MySQL, you can use the SUM() function along with the GROUP BY clause. Here's an example:


Suppose you have a table called "orders" with the following columns: order_id, customer_id, product_id, and amount.


To find the sum of the "amount" column after grouping the result by "customer_id", you can use the following query:

1
2
3
SELECT customer_id, SUM(amount) AS total_amount
FROM orders
GROUP BY customer_id;


In this query, the GROUP BY clause groups the rows by "customer_id", and the SUM() function calculates the total sum of the "amount" column for each group. The result will include the "customer_id" and the corresponding sum of the "amount" column as "total_amount".


You can modify the table name and column names in the query based on your actual table structure.