MySQL data thinning takes one piece every minute

If the original data is one piece of data every 5 seconds, and now you want to display it as one piece of data every 4 minutes, first select the rows with a remainder of 0 after dividing by 4 according to the number of minutes, and then sort within the group according to the year, month, day, and hour (window function ROW_NUMBER), and finally take out the row with serial number 1.

One per minute

with data_02 AS 
(
SELECT *
,ROW_NUMBER() OVER ( PARTITION BY DATE_FORMAT(UploadTime,'%Y-%m-%d %H:%i') ORDER BY ID) Seq 
FROM TprRecord
ORDER BY id DESC
)
SELECT * FROM data_02 WHERE Seq=1 ORDER BY id DESC LIMIT 500;

The following is one every 4 minutes

WITH data_01 AS
(
SELECT *
FROM TprRecord tr 
 WHERE MOD(MINUTE(uploadtime),4)=0
ORDER BY id DESC
),
data_02 AS 
(
SELECT *
,ROW_NUMBER() OVER ( PARTITION BY DATE_FORMAT(UploadTime,'%Y-%m-%d %H:%i') ORDER BY ID) Seq 
FROM data_01
)
SELECT * FROM data_02 WHERE Seq=1 ORDER BY id DESC LIMIT 150 ;

Insert image description here

Guess you like

Origin blog.csdn.net/qq_34677276/article/details/131920987