V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
V2EX  ›  wenxueywx  ›  全部回复第 10 页 / 共 10 页
回复总数  182
1  2  3  4  5  6  7  8  9  10  
2023 年 3 月 29 日
回复了 godleon 创建的主题 程序员 请教一个数据处理的需求,寻一个高效率的解决方法?
@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 |
+------------+------+-------+---------------------+--------------+
2023 年 3 月 28 日
回复了 godleon 创建的主题 程序员 请教一个数据处理的需求,寻一个高效率的解决方法?
期望的数据有问题吧,3-22 的 type 为 1 的数据有两条。如果确认的话窗口函数确实可以解决
1  2  3  4  5  6  7  8  9  10  
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3889 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 05:14 · PVG 13:14 · LAX 22:14 · JFK 01:14
♥ Do have faith in what you're doing.