MySQL社区

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索

mysql 复杂统计,当前遗漏,历史最大遗漏

查看数: 1838 | 评论数: 2 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2015-9-15 11:11

正文摘要:

回复

KaIIYa 发表于 2015-9-15 15:54:28
根据楼主给出的结果,可以推测是要统计在qh有序的情况下两个kjh为包含01,02,03,04,05之间的所有kjh不是01,02,03,04,05组成的所有记录数,判断出最大值(历史最大遗漏)和距离最近包含的01,02,03,04,05的所有记录数(当前遗漏)

应该是这个意思吧,用普通sql没想到怎么处理,用mysql的存储过程写了一个统计最大值的(存储过程写的很矬,请不要嘲笑)

create table t_one(
        RecNo int primary key auto_increment,
        QH int unique not null,
        KJH varchar(14) not null
);
-- 这个表存储了楼主截图中的数据

drop procedure if exists simpleCount;

create procedure simpleCount(out param int)
BEGIN
        declare temp int default 0;
        declare tempTotal int default 0;
        declare tmpKjh varchar(20);
        declare done int default 0;
        declare rs cursor for select kjh from t_one order by qh desc;
        declare continue handler for sqlstate '02000' set done=1;
       
        open rs;
        repeat
                fetch next from rs into tmpKjh;
                if not done then
                        if tmpKjh regexp '0[1-5](-0[1-5]){4}' then -- 这个正则为了判断那个无序包含01,02,03,04,05的条件
                                if temp >= tempTotal then
                                        set tempTotal= temp;
                                        set temp = 0;
                                end if;
                        else
                                set temp=temp+1;
                        end if;
                end if;
                until done end repeat;
        close rs;
        set param = tempTotal;
        if temp >= tempTotal then
                set param = temp;
        end if;
END

点评

好认真的回答,好赞!  发表于 2015-9-15 17:24
kider 发表于 2015-9-15 12:31:10
什么个意思?怎么算遗漏?

QQ|申请友链|小黑屋|Archiver|手机版|MySQL社区 ( 京ICP备07012489号   
联系人:周生; 联系电话:13911732319

GMT+8, 2024-5-19 20:35 , Processed in 0.069853 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表