求某列按分组连续编号的最大区间差异值
来源:互联网 发布:gtp7吉他谱软件下载 编辑:程序博客网 时间:2024/04/29 09:30
求一句SQL,求连续记录的最大个数
create table #t
(_id varchar(3), _date int)
insert #t select '001',20100101
union all select '001',20100102
union all select '002',20100103
union all select '002',20100101
union all select '002',20100102
union all select '002',20100104
union all select '001',20100105
union all select '001',20100106
union all select '001',20100107
union all select '002',20100105
union all select '002',20100107
union all select '002',20100108
union all select '003',20100101
union all select '003',20100102
union all select '003',20100104
union all select '003',20100105
union all select '003',20100106
union all select '003',20100108
union all select '003',20100109
union all select '003',20100111
要求: 求出每个ID 的最大连续记录的个数。记录有几百万, 效率要好一点。 谢谢!
结果为:001最大连续为从20100105到20100107,数量为3,
002最大连续为从20100101到20100104,数量为4,
003最大连续为从20100104到20100106,数量为3
===================
001 3
002 4
003 3
===================
如果不影响效率的话,最好把时间段也带上:
=================================
001 3 20100105 20100107
002 4 20100101 20100104
003 3 20100104 20100106
=================================
要能加条件, 比如查询连续数量大于3的,结果只有一条
======================================
002 4 20100101 20100104
--SQL2000
if OBJECT_ID('tempdb..#t') is not null drop table #t
go
create table #t
(id varchar(3), date int)
insert #t select '001',20100101
union all select '001',20100102
union all select '002',20100103
union all select '002',20100101
union all select '002',20100102
union all select '002',20100104
union all select '001',20100105
union all select '001',20100106
union all select '001',20100107
union all select '002',20100105
union all select '002',20100107
union all select '002',20100108
union all select '003',20100101
union all select '003',20100102
union all select '003',20100104
union all select '003',20100105
union all select '003',20100106
union all select '003',20100108
union all select '003',20100109
union all select '003',20100111
--1.
alter table #t add d int
go
declare @i int, @j int,@k int
update #t set
d = @j,
@j = case when @i = id then @j else isnull(@j,0)+1 end,
@i = id
--2.
if OBJECT_ID('tempdb..#') is not null drop table #
go
select *,0 c into # from #t order by d,date
go
declare @i int, @j int,@k int
update # set
c = @j,
@j = case when @i = id and @k=date-1 then @j +1 else 1 end,
@i = id,
@k=dateselect ID,MAX(c) 最大连续数量 from # group by ID
/*
ID 最大连续数量
---- -----------
001 3
002 4
003 3
(3 行受影响)
*/
--SQL2005:
--1.更新分区段的行
declare @i int, @j int,@k int
update #t set d = @j,@j = case when @i = _id then @j else isnull(@j,0)+1 end,@i = _id
--2.查询
;with t1 as
(
select _id,d, _date1 = _date - row_number() over (partition by d order by _date),_date
from #t
)
,t2 as
(
select _id, d, cnt = count(1),mindate=MIN(_date),maxdate= MAX(_date)
from t1
group by _id, d, _date1) select _id, maxcnt = max(cnt),
mindate=(select mindate from t2 where cnt=max(t.cnt) and _id=t._id),
maxdate=(select maxdate from t2 where cnt=max(t.cnt) and _id=t._id)
from t2 t
group by _id
/*
_id maxcnt mindate maxdate
---- ----------- ----------- -----------
001 3 20100105 20100107
002 4 20100101 20100104
003 3 20100104 20100106
(3 行受影响)
*/
原贴:http://topic.csdn.net/u/20100402/17/1565fb1e-f1e0-4961-b0c4-ae41f95c550f.html?21618
- 求某列按分组连续编号的最大区间差异值
- 邮票面值,求最大的连续区间
- 最大连续区间和的算法总结
- 最大连续区间和算法、
- 编程求邮资最大的一个连续的区间
- 求数组中连续区间的和最大
- 贪心算法-求区间至少连续k的最大和
- 求最大连续区间和的几种方法
- Oracle求连续区间内的最大最小值
- SQL 连续任意区间段分组统计
- HDU-1806 Frequent values (RMQ, 最大连续值区间)
- [VB.NET]用SQL查询得到(按编号分组的日期最大的记录)
- POJ 题目2750 Potted Flower(线段树求环型区间中连续区间的最大和)
- 连续值分组
- 在论坛中出现的比较难的sql问题:4(row_number函数+子查询 分组连续编号问题)
- 连续子段的差异
- 【贪心\最大连续区间和】游览路线
- hrbust 1684 最大连续和(区间合并)
- php的MySQL连接类
- 程序员修炼之道--资源篇
- 由感而发
- 求知
- 员工管理和激励的十个经典故事
- 求某列按分组连续编号的最大区间差异值
- 分手的这一刻,我哭了...
- 微软MVP获奖感想
- C/C++命令行参数
- 安家
- s3c2440 DM9000驱动移植
- 中国古代十大情诗
- POJ 字母旋转游戏
- 浅谈编码方式