如果实现这种查询了?

zero1977 2004-06-26 01:04:29
表结构有一个日期型的字段opdate(操作日期)
想建立一下查询:
找出连续10天都有操作的记录???

怎么做呀?那位大侠帮帮忙?
...全文
107 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghuixue 2004-06-27
  • 打赏
  • 举报
回复
up
ocean9b 2004-06-26
  • 打赏
  • 举报
回复
如写成固定语句可以这样写:
select opdate from 表 A where
exists (select opdate from 表 where opdate=dateadd(Day,1,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,2,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,3,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,4,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,5,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,6,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,7,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,8,A.opdate))
and exists (select opdate from 表 where opdate=dateadd(Day,9,A.opdate))
order by opdate

与本身的一天加起来刚好10天
ocean9b 2004-06-26
  • 打赏
  • 举报
回复
declare @n as int
declare @max_n as int
declare @sqlstr as varchar(8000)

select @max_n=10
select @n=1
select @sqlstr=''

while(@n<@max_n)
begin
select @sqlstr=@sqlstr + ' and exists (select opdate from 表 where opdate=dateadd(Day,'+ cast(@n as varchar(10)) +',A.opdate))'
select @n=@n+1
end

select @sqlstr='select opdate from 表 A where' + @sqlstr+' order by opdate'
select @sqlstr=Replace(@sqlstr,'where and','where')

execute(@sqlstr)
zero1977 2004-06-26
  • 打赏
  • 举报
回复
sorry,结果是:
2004-06-05
2004-06-06
2004-06-07
2004-06-08
2004-06-09
zero1977 2004-06-26
  • 打赏
  • 举报
回复
比如表中有如下记录:
opdate
2004-06-01
2004-06-01
2004-06-02
2004-06-03
2004-06-05
2004-06-06
2004-06-06
2004-06-07
2004-06-08
2004-06-09
2004-06-09
2004-06-10
2004-06-11
2004-06-12
2004-06-17
2004-06-18

如果要选择连续5天都有操作的话,查询结果如下:
2004-06-05
2004-06-06
2004-06-07
2004-06-09
2004-06-10

mulintaomulintao 2004-06-26
  • 打赏
  • 举报
回复
不好意思,掉了count,zjcxc写的是正确地

select * from 表 a
where (
select count(distinct opdate) from 表 where datediff(day,a.opdate,opdate) between -9 and 9)>=10
zero1977 2004-06-26
  • 打赏
  • 举报
回复
不对
mulintaomulintao 2004-06-26
  • 打赏
  • 举报
回复
select * from 表
where (
select distinct opdate from 表 where datediff(day,a.opdate,opdate) between -9 and 9)>=10

不知是否正确,因为你的意思不是很明白
zjcxc 元老 2004-06-26
  • 打赏
  • 举报
回复
select * from 表 a
where (
select count(distinct opdate) from 表 where datediff(day,a.opdate,opdate) between -9 and 9)>=10

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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