I am trying to perform two different SUMs within a single query.
user points date_achieved
-----------------------------
Foo 10 2000-01-01
Foo 20 2011-11-18
Bar 10 2000-01-01
Bar 20 2011-11-15
Bar 30 2011-11-18
1) Total points
SELECT user, SUM(points) AS points_total
FROM myTable
GROUP BY user
2) Points accumulated within the past month
SELECT user, SUM(points) AS points_month
FROM myTable
WHERE date_achieved >= CURRENT_DATE - INTERVAL 1 MONTH
GROUP BY user
What is the best way to achieve this with a single query?
Many thanks.
Just use a case statement to only count points when it meets your criteria.