Here's my current query:
SELECT IFNULL(sum(open_for), 0) total, count(IF(open_for > 0, 1, null)) wins, count(IF(open_for < 0, 1, null)) losses FROM `sport_points` WHERE (sportable_id = 1 and sportable_type = 'Team' and game_time > '2010-07-13 11:39:58 UTC'
It basically returns this aggregated data:
TEAM A
- open_for
- total: 2000
- wins: 20
- losses: 12
Now, imagine there are about 6 other columns in the table that I need execute separate queries to get all the column-specific aggregate data for one team. For example:
SELECT IFNULL(sum(FINAL_FOR), 0) total, count(IF(open_for > 0, 1, null)) wins, count(IF(open_for < 0, 1, null)) losses FROM `sport_points` WHERE (sportable_id = 1 and sportable_type = 'Team' and game_time > '2010-07-13 11:39:58 UTC'
TEAM A
- final_for
- total: 4000
- wins: 40
- losses: 18
The problem with this approach is that I have to run about 6 separate queries for all the columns on well over 200 teams. It's a serious load problem.
Ideally, the query would return all the column-specific aggregate data for one team -- in one query. It would look like this in the result:
TEAM A
- open_for_total
- open_for_wins
- open_for_losses
- final_for_total
- final_for_wins
- final_for_losses
...etc...