首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求连续值得行数统计,该怎么解决

2012-04-20 
求连续值得行数统计表aa结构如下:----------bmGM(数字)----------20110102011021201103120110412011051201

求连续值得行数统计
表aa结构如下:
----------
bm GM(数字)
----------
201101 0
201102 1
201103 1
201104 1
201105 1
201106 0
201107 0
201108 0
200109 1
----------
我要查询连续的1 或0 的最大连续数量
以上最大的连续数量为4 ,如何用sql语句查询出来

[解决办法]

SQL code
if object_id('[TB]') is not null drop table [TB]gocreate table [TB] (bm int,GM int)insert into [TB]select 201101,0 union allselect 201102,1 union allselect 201103,1 union allselect 201104,1 union allselect 201105,1 union allselect 201106,0 union allselect 201107,0 union allselect 201108,0 union allselect 200109,1select * from [TB]select TB.gm,(MAX(TB.bm)-MIN(B.bm))+1  --加1是弥补本身起始值from TBinner join TB B on TB.bm = B.bm+1 and TB.GM = B.gmgroup by TB.gm/*0    31    4*/ 

热点排行
Bad Request.