@
godleon 特意用 mysql8.0 试了下纯 sql 的方法
CREATE TABLE `t01` (
`id` int NOT NULL AUTO_INCREMENT,
`type` int DEFAULT NULL,
`value` double DEFAULT NULL,
`date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB ;
INSERT INTO `t01` VALUES (1,1,10.111,'2023-03-21 01:00:00'),(2,2,19.111,'2023-03-21 01:00:00'),(3,2,11.111,'2023-03-21 02:00:00'),(4,3,12.111,'2023-03-21 02:00:00'),(5,1,13.111,'2023-03-22 11:00:00'),(6,1,14.111,'2023-03-22 12:00:00'),(7,1,15.111,'2023-03-23 12:00:00');
select distinct date(`date`) as`date`,`type`,count(*) over(partition by date(`date`),`type`) as count, first_value(`date`) over (partition by date(`date`),`type` order by `date` desc) as `latest_datetime`,first_value(`value`) over (partition by date(`date`),`type` order by `date` desc) as `latest_value` from t01;
+------------+------+-------+---------------------+--------------+
| date | type | count | latest_datetime | latest_value |
+------------+------+-------+---------------------+--------------+
| 2023-03-21 | 1 | 1 | 2023-03-21 01:00:00 | 10.111 |
| 2023-03-21 | 2 | 2 | 2023-03-21 02:00:00 | 11.111 |
| 2023-03-21 | 3 | 1 | 2023-03-21 02:00:00 | 12.111 |
| 2023-03-22 | 1 | 2 | 2023-03-22 12:00:00 | 14.111 |
| 2023-03-23 | 1 | 1 | 2023-03-23 12:00:00 | 15.111 |
+------------+------+-------+---------------------+--------------+