heswara
heswara

Reputation: 11

Get total ans subtotal with MYSQL QUERY and GROUP BY

In SQL I want to get total by id. Please suggest me for the query

INPUT

id | qty | price
1  | 3   | 200
1  | 4   | 225
1  | 5   | 250
2  | 7   | 300
2  | 8   | 300
3  | 3   | 500
3  | 5   | 500
3  | 6   | 500
4  | 3   | 700
4  | 2   | 745

OUTPUT

id | total
1  | 2750
2  | 4500
3  | 7000
4  | 3590

Upvotes: 0

Views: 51

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520918

This query sums the quantity times price for all records having the same id, for each id value in your table.

SELECT id,
       SUM(qty*price) AS total
FROM yourTable
GROUP BY id

Upvotes: 2

Related Questions