Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

MySQL substract first row from last row of a group

I'm working on creating Google Charts from MySQL datasource. This works fine so far. Now I want to count how many blocks of a Blockchain processed per hour.

How can I simple substract the first row from last row of a group?

SELECT
   date_format(time,'%Y-%m-%d %H-%i'),blocks,
   count(1)
FROM blockchain
GROUP BY 1  
ORDER BY `date_format(time,'%Y-%m-%d %H-%i')` ASC

And if this done, how to repeat this for the last 24 hours?

Sample data:

id          time                        blocks

3           2020-12-30 11:21:53         112149
4           2020-12-30 11:21:55         112150
5           2020-12-30 11:21:56         112150
6           2020-12-30 11:21:57         112150
7           2020-12-30 11:24:01         112169
8           2020-12-30 11:25:01         112178
9           2020-12-30 11:26:01         112188
10          2020-12-30 11:27:01         112197



-10         2020-12-30 11:27:01         112197
 3          2020-12-30 11:21:53         112149
-----------------------------------------------
                                        48                                       

I hope its not a stupid question. I'm kinda new to this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this query to get blocks per hour.

select hour(time) as hr, max(blocks)-min(blocks) as blocks_per_hour
from test
group by hour(time)

Result:

hr blocks_per_hour
11 48

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...