关闭

MySQL连续数统计

标签: mysql连续数统计
1262人阅读 评论(0) 收藏 举报
分类:
CREATE TABLE [dbo].Jqfk NOT NULL,
[yhh] char NULL,CREATE TABLE [dbo].Jqfk NOT NULL,
[yhh] char NULL,
[cbrq] nvarchar NULL,
[ysl] [int] NULL,
)
[cbrq] [nvarchar](19) NULL,
[ysl] [int] NULL,
)
INSERT INTO jqfk VALUES ('B173022031201','2015-01-08 10:12:30',0);
INSERT INTO jqfk VALUES ('B211001024011','2015-01-08 10:46:13',13);
INSERT INTO jqfk VALUES ('B211001024012','2015-01-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024013','2015-01-08 10:46:13',3);
INSERT INTO jqfk VALUES ('B211001024014','2015-01-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B173022031201','2015-02-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024011','2015-02-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024012','2015-02-08 10:46:13',13);
INSERT INTO jqfk VALUES ('B211001024013','2015-02-08 10:46:13',11);
INSERT INTO jqfk VALUES ('B211001024014','2015-02-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B173022031201','2015-03-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024011','2015-03-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024012','2015-03-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024013','2015-03-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024014','2015-03-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B173022031201','2015-04-08 10:46:13',13);
INSERT INTO jqfk VALUES ('B211001024011','2015-04-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024012','2015-04-08 10:46:13',12);
INSERT INTO jqfk VALUES ('B211001024013','2015-04-08 10:46:13',0);
INSERT INTO jqfk VALUES ('B211001024014','2015-04-08 10:46:13',0);

需要连续统计最新日期开始,为0的数。比如上面统计结果应该是

1 B211001024014 4
2 B211001024011 3
3 B211001024013 2


SQL如下

SELECT yhh,MAX(s) nums from (danielinbiti
select a.*
,case when @f=0 then @cust:=yhh end t1
,@f:=1
,case when yhh!=@cust then @flag:=0 end t
,case when ysl!=0 and @flag=0 then @flag:=1 end f
,case when ysl=0 and @flag=0 then @cnt:=@cnt+1 else @cnt:=0 end c
,@cnt s,@cust:=yhh,@flag f2 from (
select * from jqfk order by yhh,cbrq desc 
) a,(select @cust:='') r,(select @cnt:=0) r2,(select @flag:=0) r3,(select @f:=0) r4
) k where f2=0 group by yhh order by nums desc


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:507825次
    • 积分:5842
    • 等级:
    • 排名:第4543名
    • 原创:137篇
    • 转载:52篇
    • 译文:0篇
    • 评论:176条
    文章分类
    最新评论