有这样的一个表 里面的数据是这样的
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 这个时间段这个人的时间是否是连续的高手帮忙想个办法…………
select tt.*,
lag(endtime)over(partition by userid order by endtime)lg
from tt)
where starttime>lg
试试这个,查出不连续的记录
可以在where后继续添加时间段限制
后面的lg是什么啊
lag(endtime)over(partition by userid order by endtime)lg
endtime是字段名还是变量名啊?
lg是列别名,指代lag(endtime)over(partition by userid order by endtime)
endtime代表你表里的结束时间字段
starttime表示开始时间
我要的是 给出一个时间段 最后得到的是人的id 代表在这个时间段这个人是连续登记的.
上面的两个是登记的起始时间,登记的结束时间 后面的是人的id(1000001)
要求出在‘2005-01-01‘到’2006-01-01‘时间段内看看哪个人是连续登记的
算法比较复杂,必须得写存储过程,一个sql应该出不来
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
'2005-01-01'和'2006...'改成date'2005-01-01' date'2006..
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