日期:2014-05-17 浏览次数:20519 次
--建立测试
CREATE TABLE #CESHI (ID varchar(10),RESULT varchar(10),dt DATETIME)
--插入数据
INSERT INTO #CESHI
SELECT '1','大' ,'2012-10-01 10:01:00'
UNION
SELECT '2','大','2012-10-01 10:02:00'
UNION
SELECT'3','小', '2012-10-01 10:03:00'
UNION
SELECT'4','大', '2012-10-01 10:04:00'
UNION
SELECT'5','小', '2012-10-01 10:05:00'
UNION
SELECT'6','小', '2012-10-01 10:06:00'
UNION
SELECT'7','小', '2012-10-01 10:07:00'
UNION
SELECT'8','小', '2012-10-01 10:08:00'
UNION
SELECT'9','大', '2012-10-01 10:09:00'
UNION
SELECT'10','大', '2012-10-01 10:10:00'
--排序
select *,num=row_number() over(order by getdate())into #tb from #CESHI order by dt
--取得数据
select result,count(1) as cnt into #jg
from(
select result,num,
gid=num-(select count(1) from #tb where result=t.result and num<t.num)
from #tb t
) a
group by result,gid
order by min(num)
--输出结果
select MAX(cnt)from #jg
--删除测试
drop table #CESHI
drop table #tb
drop table #jg
--排序
select *,num=row_number() over(order by getdate())into #tb from #CESHI order by dt
;with #tb as
(select *,num=row_number() over(order by getdate()) from #CESHI )
--建立测试
CREATE TABLE #CESHI (ID varchar(10),RESULT varchar(10),dt DATETIME)
--插入数据