GROUP BY ID range?

…衆ロ難τιáo~ 提交于 2019-12-14 04:05:43

问题


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.


回答1:


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.




回答2:


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)




回答3:


group by year(your_date), week(your_date)

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



来源:https://stackoverflow.com/questions/1113896/group-by-id-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!