22,207
社区成员
发帖
与我相关
我的任务
分享
declare @table table (name varchar(10),[date] varchar(10))
insert into @table
select 'X106','2010-01-01' union all
select 'X106','2010-01-02' union all
select 'X106','2010-01-03' union all
select 'X106','2010-01-09' union all
select 'X106','2010-01-10' union all
select 'X106','2010-01-16' union all
select 'X106','2010-01-17' union all
select 'X106','2010-01-23' union all
select 'X106','2010-01-24' union all
select 'X106','2010-01-30' union all
select 'X106','2010-01-31' union all
select 'X106','2010-02-04' union all
select 'X106','2010-02-05' union all
select 'X106','2010-02-06' union all
select 'X106','2010-02-07' union all
select 'X106','2010-02-08' union all
select 'X106','2010-02-09' union all
select 'X106','2010-02-10' union all
select 'X106','2010-02-11' union all
select 'X106','2010-02-12' union all
select 'X106','2010-02-13' union all
select 'X106','2010-02-14' union all
select 'X106','2010-02-15' union all
select 'X106','2010-02-16' union all
select 'X106','2010-02-17' union all
select 'X106','2010-02-18' union all
select 'X106','2010-02-19' union all
select 'X106','2010-02-20' union all
select 'X106','2010-02-21' union all
select 'X106','2010-02-22' union all
select 'X106','2010-02-23' union all
select 'X106','2010-02-24' union all
select 'X106','2010-02-25' union all
select 'X106','2010-02-27'
select *from @table
--问题:找到不间断的日期
--以上日期返回为
/*
X106 2010-02-04
X106 2010-02-05
X106 2010-02-06
X106 2010-02-07
X106 2010-02-08
X106 2010-02-09
X106 2010-02-10
X106 2010-02-11
X106 2010-02-12
X106 2010-02-13
X106 2010-02-14
X106 2010-02-15
X106 2010-02-16
X106 2010-02-17
X106 2010-02-18
X106 2010-02-19
X106 2010-02-20
X106 2010-02-21
X106 2010-02-22
X106 2010-02-23
X106 2010-02-24
X106 2010-02-25*/
--修改
select distinct t.* from @table as t
, (
select date as mindate from @table as t
where exists(select 1 from @table as t2 where cast(t2.date as smalldatetime) between cast(t.date as smalldatetime) and dateadd(day,20,cast(t.date as smalldatetime)) having count(*)=21)
) t2
where cast(t.date as smalldatetime) between mindate and dateadd(day,21,cast(mindate as smalldatetime))
declare @table table ([name] varchar(10),[date] varchar(10))
insert into @table
select 'X106','2010-01-01' union all
select 'X106','2010-01-02' union all
select 'X106','2010-01-03' union all
select 'X106','2010-01-09' union all
select 'X106','2010-01-10' union all
select 'X106','2010-01-16' union all
select 'X106','2010-01-17' union all
select 'X106','2010-01-23' union all
select 'X106','2010-01-24' union all
select 'X106','2010-01-30' union all
select 'X106','2010-01-31' union all
select 'X106','2010-02-04' union all
select 'X106','2010-02-05' union all
select 'X106','2010-02-06' union all
select 'X106','2010-02-07' union all
select 'X106','2010-02-08' union all
select 'X106','2010-02-09' union all
select 'X106','2010-02-10' union all
select 'X106','2010-02-11' union all
select 'X106','2010-02-12' union all
select 'X106','2010-02-13' union all
select 'X106','2010-02-14' union all
select 'X106','2010-02-15' union all
select 'X106','2010-02-16' union all
select 'X106','2010-02-17' union all
select 'X106','2010-02-18' union all
select 'X106','2010-02-19' union all
select 'X106','2010-02-20' union all
select 'X106','2010-02-21' union all
select 'X106','2010-02-22' union all
select 'X106','2010-02-23' union all
select 'X106','2010-02-24' union all
select 'X106','2010-02-25' union all
select 'X106','2010-02-27'
select [name],
[date],
ID=replace([date],'-',''),
CNT=cast(replace([date],'-','')as bigint)-
(select count(*) from @table n where cast(replace(m.[date],'-','')as bigint)
>cast(replace(n.[date],'-','')as bigint))
into #
from @table m
select name,
date
from # t
where (select count(*) from # where cnt=t.cnt)>20
drop table #
/*
name date
---------- ----------
X106 2010-02-04
X106 2010-02-05
X106 2010-02-06
X106 2010-02-07
X106 2010-02-08
X106 2010-02-09
X106 2010-02-10
X106 2010-02-11
X106 2010-02-12
X106 2010-02-13
X106 2010-02-14
X106 2010-02-15
X106 2010-02-16
X106 2010-02-17
X106 2010-02-18
X106 2010-02-19
X106 2010-02-20
X106 2010-02-21
X106 2010-02-22
X106 2010-02-23
X106 2010-02-24
X106 2010-02-25
(22 個資料列受到影響)
*/
select px=identity(int,1,1),* into #1 from @table t where not exists(select 1 from @table where datediff(dd,[date],t.[date])=1)
select px=identity(int,1,1),* into #2 from @table t where not exists(select 1 from @table where datediff(dd,[date],t.[date])=-1)
go
select a.* from # a,#1 b,#2 c where
c.px=b.px and
datediff(dd,b.[date],c.[date])>=21 and
a.[date] between b.[date] and c.[date]
/**
name date
---------- ----------
X106 2010-02-04
X106 2010-02-05
X106 2010-02-06
X106 2010-02-07
X106 2010-02-08
X106 2010-02-09
X106 2010-02-10
X106 2010-02-11
X106 2010-02-12
X106 2010-02-13
X106 2010-02-14
X106 2010-02-15
X106 2010-02-16
X106 2010-02-17
X106 2010-02-18
X106 2010-02-19
X106 2010-02-20
X106 2010-02-21
X106 2010-02-22
X106 2010-02-23
X106 2010-02-24
X106 2010-02-25
(所影响的行数为 22 行)
**/
declare @table table (name varchar(10),[date] varchar(10))
insert into @table
select 'X106','2010-01-01' union all
select 'X106','2010-01-02' union all
select 'X106','2010-01-03' union all
select 'X106','2010-01-09' union all
select 'X106','2010-01-10' union all
select 'X106','2010-01-16' union all
select 'X106','2010-01-17' union all
select 'X106','2010-01-23' union all
select 'X106','2010-01-24' union all
select 'X106','2010-01-30' union all
select 'X106','2010-01-31' union all
select 'X106','2010-02-04' union all
select 'X106','2010-02-05' union all
select 'X106','2010-02-06' union all
select 'X106','2010-02-07' union all
select 'X106','2010-02-08' union all
select 'X106','2010-02-09' union all
select 'X106','2010-02-10' union all
select 'X106','2010-02-11' union all
select 'X106','2010-02-12' union all
select 'X106','2010-02-13' union all
select 'X106','2010-02-14' union all
select 'X106','2010-02-15' union all
select 'X106','2010-02-16' union all
select 'X106','2010-02-17' union all
select 'X106','2010-02-18' union all
select 'X106','2010-02-19' union all
select 'X106','2010-02-20' union all
select 'X106','2010-02-21' union all
select 'X106','2010-02-22' union all
select 'X106','2010-02-23' union all
select 'X106','2010-02-24' union all
select 'X106','2010-02-25' union all
select 'X106','2010-02-27'
select name,[date]
from
(select id = day([date])-(select count(1) from @table where name=t.name and [date]<=t.[date]),*
from @table t) r
where id in (select id from
(select id = day([date])-(select count(1) from @table where name=t.name and [date]<=t.[date]),*
from @table t)
h
group by id
having count(1) >= 21)
name date
---------- ----------
X106 2010-02-04
X106 2010-02-05
X106 2010-02-06
X106 2010-02-07
X106 2010-02-08
X106 2010-02-09
X106 2010-02-10
X106 2010-02-11
X106 2010-02-12
X106 2010-02-13
X106 2010-02-14
X106 2010-02-15
X106 2010-02-16
X106 2010-02-17
X106 2010-02-18
X106 2010-02-19
X106 2010-02-20
X106 2010-02-21
X106 2010-02-22
X106 2010-02-23
X106 2010-02-24
X106 2010-02-25
(所影响的行数为 22 行)
select t.* from @table as t
, (
select min(date) as mindate from @table as t
where exists(select 1 from @table as t2 where cast(t2.date as smalldatetime) between cast(t.date as smalldatetime) and dateadd(day,20,cast(t.date as smalldatetime)) having count(*)=21)
) t2
where cast(t.date as smalldatetime) between mindate and dateadd(day,20,cast(mindate as smalldatetime))