views:

474

answers:

3

Given a data set like this;

+-----+---------------------+--------+
| id  | date                | result |
+-----+---------------------+--------+
| 121 | 2009-07-11 13:23:24 |     -1 | 
| 122 | 2009-07-11 13:23:24 |     -1 | 
| 123 | 2009-07-11 13:23:24 |     -1 | 
| 124 | 2009-07-11 13:23:24 |     -1 | 
| 125 | 2009-07-11 13:23:24 |     -1 | 
| 126 | 2009-07-11 13:23:24 |     -1 | 
| 127 | 2009-07-11 13:23:24 |     -1 | 
| 128 | 2009-07-11 13:23:24 |     -1 | 
| 129 | 2009-07-11 13:23:24 |     -1 | 
| 130 | 2009-07-11 13:23:24 |     -1 | 
| 131 | 2009-07-11 13:23:24 |     -1 | 
| 132 | 2009-07-11 13:23:24 |     -1 | 
| 133 | 2009-07-11 13:23:24 |     -1 | 
| 134 | 2009-07-11 13:23:24 |     -1 | 
| 135 | 2009-07-11 13:23:24 |     -1 | 
| 136 | 2009-07-11 13:23:24 |     -1 | 
| 137 | 2009-07-11 13:23:24 |     -1 | 
| 138 | 2009-07-11 13:23:24 |      1 | 
| 139 | 2009-07-11 13:23:24 |      0 | 
| 140 | 2009-07-11 13:23:24 |     -1 | 
+-----+---------------------+--------+

How would I go about grouping the results by day 5 records at a time. The above results is part of the live data, there is over 100,000 results rows in the table and its growing. Basically I want to measure the change over time, so want to take a SUM of the result every X records. In the real data I'll be doing it ever 100 or 1000 but for the data above perhaps every 5.

If i could sort it by date I would do something like this;

SELECT
   DATE_FORMAT(date, '%h%i') ym,
   COUNT(result) 'Total Games', 
   SUM(result) as 'Score'
FROM nn_log
GROUP BY ym;

I can't figure out a way of doing something similar with numbers. The order is sorted by the date but I hope to split the data up every x results. It's safe to assume there are no blank rows.

Doing it above with the data you could do multiple selects like;

SELECT SUM(result) FROM table LIMIT 0,5;
SELECT SUM(result) FROM table LIMIT 5,5;
SELECT SUM(result) FROM table LIMIT 10,5;

Thats obviously not a very good way to scale up to a bigger problem. I could just write a loop but I'd like to reduce the number of queries.

+3  A: 

You can use integer division on the ROWNUMBER to do this.

If you are CERTAIN that your id column is consecuitive just use: GROUP BY FLOOR(id/5)

John Gietzen
+3  A: 

How about...

SELECT
   floor(id / 5) ym,
   COUNT(result) 'Total Games', 
   SUM(result) as 'Score'
FROM nn_log
GROUP BY ym;

(I'm assuming that the id is correlative)

This is the same idea in your query, only using the ID to group instead of the day.

Daniel Magliola
Perfect thanks.Interestingly doing that gave me only 999 for the first one... but I can live with that.mysql> SELECT -> floor(id / 1000) ym, -> COUNT(result) 'Total Games', -> SUM(result) as 'Score' -> FROM nn_log -> GROUP BY ym;+------+-------------+-------+| ym | Total Games | Score |+------+-------------+-------+| 0 | 999 | -879 | | 1 | 1000 | -889 | | 2 | 1000 | -920 | | 3 | 1000 | -903 | ....
Orange Box
d'oh - formatting fail lol
Orange Box
A: 

group by year(your_date), week(your_date)

that will group by 7 day intervals which might work for you.

f00