MySQL: 划分具有连续相同值的区间

需求#

有一组数据,第一列是类型A/B,第二列是操作时间,怎么取出不同类型不同时间段的最大、最小操作时间?
输入:
image
输出:
image

建表语句:

create table test2 (
	`type` varchar(5),
	`time` timestamp
)engine=innodb;

insert into test2
values
('A', '2021-08-09 10:00:00'),
('A', '2021-08-09 10:04:00'),
('A', '2021-08-09 10:04:06'),
('B', '2021-08-12 10:04:00'),
('B', '2021-08-13 10:04:00'),
('A', '2021-08-14 10:04:00'),
('A', '2021-08-15 10:08:00'),
('B', '2021-08-16 10:04:00'),
('B', '2021-08-16 10:04:00'),
('B', '2021-08-18 10:04:00')

解决#

select distinct
    type,
    min_type,
    max_type
from (
    select
        `type`,
        `time`,
        max(`time`) over (partition by flag, `type`) max_type,
        min(`time`) over (partition by flag, `type`) min_type
    from (
        select
            a.*,
            cast(rn2_type as signed) - cast(rn_time as signed) as flag
        from (
            select
                `type`,
                `time`,
                row_number() over (order by `time`) rn_time,
                row_number() over (partition by `type` order by `time`) rn2_type
            from test2
        ) a
    ) a
) a

过程解析#

嵌套sql从最里层开始看,非数值连续类型铺垫,先设置两个标签rn_time,rn_type:

select 
	`type`,
	`time`,
	row_number() over(order by time ) rn_time,
	row_number() over(partition by type order by time) rn2_type
from test2

image

rn_time,rn_type相减得到连续类型分组:

select
    a.*,
    cast(rn2_type as signed) - cast(rn_time as signed) as flag
from (
    select
        `type`,
        `time`,
        row_number() over (order by `time`) rn_time,
        row_number() over (partition by type order by `time`) rn2_type
    from test2
) a

image

求分组最大值和最小值(注意不同type的flag列可能会相同,因此需要 partition by flag, type参考的原文中这里有bug):

select
    `type`,
    `time`,
    max(`time`) over (partition by flag, `type`) max_type,
    min(`time`) over (partition by flag, `type`) min_type
from (
    select
        a.*,
        cast(rn2_type as signed) - cast(rn_time as signed) as flag
    from (
        select
            `type`,
            `time`,
            row_number() over (order by `time`) rn_time,
            row_number() over (partition by type order by `time`) rn2_type
        from test2
    ) a
) a

image

最后一步:去重:

select distinct
    type,
    min_type,
    max_type
from (
    select
        `type`,
        `time`,
        max(`time`) over (partition by flag, `type`) max_type,
        min(`time`) over (partition by flag, `type`) min_type
    from (
        select
            a.*,
            cast(rn2_type as signed) - cast(rn_time as signed) as flag
        from (
            select
                `type`,
                `time`,
                row_number() over (order by `time`) rn_time,
                row_number() over (partition by `type` order by `time`) rn2_type
            from test2
        ) a
    ) a
) a

来源#

本文来源:https://blog.csdn.net/qq_34019697/article/details/114578668
文中有部分错误,已在本文修改。

posted @   拾月凄辰  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2022-12-04 如何清理C盘空间
2022-12-04 如何使用PowerShell批量删除注册表项
2021-12-04 Ubuntu 安装 Insomnia
2021-12-04 Ubuntu 安装 http-server
2021-12-04 Golang 中的反向代理(ReverseProxy) 介绍与使用
2021-12-04 git fetch 的作用与原理
点击右上角即可分享
微信分享提示
主题色彩