假如原始数据为每5秒一个数据,现在想展示为每4分钟一条数据,先按照分钟数把除以4余数为0的行选出来,在按照 年月日 时分,做组内排序(窗函数ROW_NUMBER),最后再拿出序号为1的行。
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 ;