日期不间断查询(请高手帮忙)

duanzhi1984 2010-03-10 04:07:44
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*/


请高手帮忙.感谢

...全文
145 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
allen750 2010-03-10
  • 打赏
  • 举报
回复
太强了,终于看明了
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)
csdyyr 2010-03-10
  • 打赏
  • 举报
回复
--修改
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))
duanzhi1984 2010-03-10
  • 打赏
  • 举报
回复
引用 13 楼 csdyyr 的回复:
引用 12 楼 duanzhi1984 的回复:引用 7 楼 csdyyr 的回复: SQL codeselect t.*from@tableas t , (selectmin(date)as mindatefrom@tableas twhereexists(select1from@tableas t2wherecast(t2.dateassmalldatetime)betweencast(t.dateassmalldatetime)anddateadd(day,20,cast(t?- 你的查询有点问题,查询中无 2010-02-25
最后一行:20->21

谢谢!!!!!!!!
非常感谢各位
csdyyr 2010-03-10
  • 打赏
  • 举报
回复
引用 12 楼 duanzhi1984 的回复:
引用 7 楼 csdyyr 的回复:
SQL codeselect t.*from@tableas t
, (selectmin(date)as mindatefrom@tableas twhereexists(select1from@tableas t2wherecast(t2.dateassmalldatetime)betweencast(t.dateassmalldatetime)anddateadd(day,20,cast(t?-
你的查询有点问题,查询中无

2010-02-25

最后一行:20->21
duanzhi1984 2010-03-10
  • 打赏
  • 举报
回复
引用 7 楼 csdyyr 的回复:
SQL codeselect t.*from@tableas t
, (selectmin(date)as mindatefrom@tableas twhereexists(select1from@tableas t2wherecast(t2.dateassmalldatetime)betweencast(t.dateassmalldatetime)anddateadd(day,20,cast(t?-

你的查询有点问题,查询中无

2010-02-25
水族杰纶 2010-03-10
  • 打赏
  • 举报
回复
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 個資料列受到影響)

*/
sql_db 2010-03-10
  • 打赏
  • 举报
回复
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 行)

**/
nianran520 2010-03-10
  • 打赏
  • 举报
回复
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 行)
duanzhi1984 2010-03-10
  • 打赏
  • 举报
回复
我们的服务器是2000的,能用2000的搞一个吗?
csdyyr 2010-03-10
  • 打赏
  • 举报
回复
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))
-狙击手- 2010-03-10
  • 打赏
  • 举报
回复
引用 4 楼 duanzhi1984 的回复:
引用 1 楼 wufeng4552 的回复:select 'X106','2010-01-01' union all select 'X106','2010-01-02' union all select 'X106','2010-01-03' union all 這也連續阿威什麼不算?
连续21天以上的记录。。
谢谢


;with cte
as
(select rid=row_number() over (order by getdate()),* from @table)
select name,[date]
from cte where cast([date] as int)- rid in (
select cast([date] as int)- rid as rid from cte group by cast([date] as int)- rid having count(1) >21)
-狙击手- 2010-03-10
  • 打赏
  • 举报
回复
;with cte
as
(select rid=row_number() over (order by getdate()),* from @table)
select name,[date]
from cte where cast([date] as int)- rid in (
select cast([date] as int)- rid as rid from cte group by cast([date] as int)- rid having count(1) > 5)
duanzhi1984 2010-03-10
  • 打赏
  • 举报
回复
引用 1 楼 wufeng4552 的回复:
select 'X106','2010-01-01' union all
select 'X106','2010-01-02' union all
select 'X106','2010-01-03' union all
這也連續阿
威什麼不算?

连续21天以上的记录。。
谢谢
duanzhi1984 2010-03-10
  • 打赏
  • 举报
回复
哦,不好意思,

连续21天以上的记录。。
谢谢
sql_db 2010-03-10
  • 打赏
  • 举报
回复
2-4到2-25不间断?
水族杰纶 2010-03-10
  • 打赏
  • 举报
回复
select 'X106','2010-01-01' union all
select 'X106','2010-01-02' union all
select 'X106','2010-01-03' union all
這也連續阿
威什麼不算?

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧