matt
matt

Reputation: 2089

MySQL grouping and summing

I have a query that returns a table that looks something like this:

+------+----------+-------+------+----+
| pID  | name     | month | q    | s  |
+------+----------+-------+------+----+
| 1468 | bob      |     2 |    1 | 14 |
| 1469 | bob      |     2 |    1 |  2 |
| 1470 | bob      |     2 |    1 |  9 |
| 1468 | bob      |     3 |    1 |  7 |
| 1469 | bob      |     3 |    1 |  8 |
| 1470 | bob      |     3 |    1 | 11 |
+------+----------+-------+------+----+

and I would like the output to be

+----------+-------+------+-----+
| name     | month | q    | sub |
+----------+-------+------+-----+
| bob      |     2 |    1 |  25 |
| bob      |     3 |    1 |  26 |
+----------+-------+------+-----+

Essentially, I want the first two columns of my output to be name, month and q grouped by name and month (they will always have the same data per line in this grouping) and I want the last column to be the SUM of s grouped by name only.

Thanks.

Upvotes: 0

Views: 37

Answers (1)

JanR
JanR

Reputation: 6130

It should be something like this:

SELECT name, month, q, SUM(sub)
FROM table
GROUP BY name, month, q

Upvotes: 2

Related Questions