没有分了,不好意思。求一个时间的判断方法。

qiang521qiang 2009-09-18 03:22:45

有这样的一个表 里面的数据是这样的
1 2003-7-1 2004-3-31 1000001 233
2 2004-1-1 2004-6-30 1000001 234
3 2004-4-1 2004-6-30 1000001 235
4 2004-7-1 2004-8-15 1000001 236
5 2005-6-1 2005-6-30 1000001 237
6 2005-7-1 2005-12-31 1000001 238
7 2005-7-1 2006-3-31 1000001 239
8 2005-7-1 2006-6-30 1000001 240
9 2006-7-1 2006-12-31 1000001 241
10 1997-3-1 1997-7-1 1000001 242
11 1998-1-1 1998-7-1 1000001 243
12 1998-7-1 1998-12-31 1000001 244

前面的那个时间是起始的时间,后面的那个是结束的时间。 怎么能判断在一个时间段内他是否是连续的。
意思就是 比如说 2005-1-1 到 2005-7-1 这个时间段这个人的时间是否是连续的

高手帮忙想个办法…………
...全文
130 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
小灰狼W 2009-09-18
  • 打赏
  • 举报
回复
select userid from(
select tt.*,
lag(endtime)over(partition by userid order by endtime)lg
from tt)
where starttime between date'2005-01-01' and date'2006-01-01'
group by userid
having max(case when starttime>lg then 1 end) is null
小灰狼W 2009-09-18
  • 打赏
  • 举报
回复
时间字段是date类型的?
'2005-01-01'和'2006...'改成date'2005-01-01' date'2006..
小灰狼W 2009-09-18
  • 打赏
  • 举报
回复
select userid from(
select tt.*,
lag(endtime)over(partition by userid order by endtime)lg
from tt)
where starttime between '2005-01-01' and '2006-01-01'
group by userid
having max(case when starttime>lg then 1 end)=0
lonsee 2009-09-18
  • 打赏
  • 举报
回复
个人意见。。
算法比较复杂,必须得写存储过程,一个sql应该出不来。。。
qiang521qiang 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wildwave 的回复:]
引用 5 楼 qiang521qiang 的回复:
引用 4 楼 wildwave 的回复:
select * from(
    select tt.*,
      lag(endtime)over(partition by userid order by endtime)lg
    from tt)
where starttime>lg
试试这个,查出不连续的记录
可以在where后继续添加时间段限制

lag(endtime)over(partition by userid order by endtime)lg
后面的lg是什么啊
lag(endtime)over(partition by userid order by endtime)lg
endtime是字段名还是变量名啊?

你都不给字段名,我就随便取了一个
lg是列别名,指代lag(endtime)over(partition by userid order by endtime)
endtime代表你表里的结束时间字段
starttime表示开始时间
[/Quote]
好像不是那么回事
我要的是 给出一个时间段 最后得到的是人的id 代表在这个时间段这个人是连续登记的.
上面的两个是登记的起始时间,登记的结束时间 后面的是人的id(1000001)
要求出在‘2005-01-01‘到’2006-01-01‘时间段内看看哪个人是连续登记的
小灰狼W 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qiang521qiang 的回复:]
引用 4 楼 wildwave 的回复:
select * from(
    select tt.*,
      lag(endtime)over(partition by userid order by endtime)lg
    from tt)
where starttime>lg
试试这个,查出不连续的记录
可以在where后继续添加时间段限制

lag(endtime)over(partition by userid order by endtime)lg
后面的lg是什么啊
lag(endtime)over(partition by userid order by endtime)lg
endtime是字段名还是变量名啊?
[/Quote]
你都不给字段名,我就随便取了一个
lg是列别名,指代lag(endtime)over(partition by userid order by endtime)
endtime代表你表里的结束时间字段
starttime表示开始时间
qiang521qiang 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wildwave 的回复:]
select * from(
    select tt.*,
      lag(endtime)over(partition by userid order by endtime)lg
    from tt)
where starttime>lg
试试这个,查出不连续的记录
可以在where后继续添加时间段限制
[/Quote]
lag(endtime)over(partition by userid order by endtime)lg
后面的lg是什么啊
lag(endtime)over(partition by userid order by endtime)lg
endtime是字段名还是变量名啊?
小灰狼W 2009-09-18
  • 打赏
  • 举报
回复
select * from(
select tt.*,
lag(endtime)over(partition by userid order by endtime)lg
from tt)
where starttime>lg
试试这个,查出不连续的记录
可以在where后继续添加时间段限制
qiang521qiang 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 oraclemch 的回复:]
你时间段连续不连续的规律是什么,是按天数来判断吗?
[/Quote]

那个时间是这样的 第一条记录 2003-07-01 2004-03-31 说明 03年7月1号 到04年 3月 31号是连续的
第二条 2004-1-1 2004-6-30 说明 04年 1月1号到 04年 6月 1号是连续的
通过前两条 就说明 从2003-07-01 到 2004-6-30 这个时间段是连续的
后面的1000001是人的序号 233是id
是判断在某个时间段 看看哪些人是连续登记的
上面的那些时间都是 登记时间
大家明白没有啊
小灰狼W 2009-09-18
  • 打赏
  • 举报
回复
怎么样算连续呢
1000001 233是什么
oraclemch 2009-09-18
  • 打赏
  • 举报
回复
你时间段连续不连续的规律是什么,是按天数来判断吗?

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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