日期:2014-05-18  浏览次数:20621 次

如何判断查询结果中日期的连续
"name" "date"
"(?)尾人柱力" "2012-02-18"
"(?)尾人柱力" "2012-02-19"
"(?)尾人柱力" "2012-02-20"
"(?)尾人柱力" "2012-02-21"
"(?)尾人柱力" "2012-02-22"
"(?)尾人柱力" "2012-02-23"
"(?)尾人柱力" "2012-02-24"
"(眞愛苛笶." "2012-02-18"
".惹关关" "2012-02-18"
".苍井空." "2012-02-18"
".苍井空." "2012-02-19"
".苍井空." "2012-02-20"
".苍井空." "2012-02-21"
".苍井空." "2012-02-22"
".苍井空." "2012-02-23"
".苍井空." "2012-02-24"
"//。紀寧ggく" "2012-02-18"
"//。紀寧ggく" "2012-02-21"
"00xx111" "2012-02-18"
"00xx111" "2012-02-19"



SQL code

select name,date(time) date from logdb.player_log3 where time BETWEEN '2012-02-18' and '2012-02-25' group by name,date limit 20;



取出日期之后如何判断每个玩家最大的连续天数,求教!

------解决方案--------------------
SQL code

GO
IF OBJECT_ID('TBL')IS NOT NULL
DROP TABLE TBL
GO
CREATE TABLE TBL(
日期 DATE
)
GO
INSERT TBL
SELECT '2012-03-01' UNION ALL
SELECT '2012-03-31'


--利用递归实现输出三月份的所有日期:
go
declare @date date
select @date=MAX(日期) from tbl
;with t
as(
select * from tbl
union all
select dateadd(dd,1,a.日期) from t a
where not exists(select * from tbl b
where b.日期=DATEADD(DD,1,a.日期)
)
and a.日期<@date
)
select *from t order by 日期

/*
日期
2012-03-01
2012-03-02
2012-03-03
2012-03-04
2012-03-05
2012-03-06
2012-03-07
2012-03-08
2012-03-09
2012-03-10
2012-03-11
2012-03-12
2012-03-13
2012-03-14
2012-03-15
2012-03-16
2012-03-17
2012-03-18
2012-03-19
2012-03-20
2012-03-21
2012-03-22
2012-03-23
2012-03-24
2012-03-25
2012-03-26
2012-03-27
2012-03-28
2012-03-29
2012-03-30
2012-03-31
*/

查询出不存在的日期和原有的日期
,自动生成三月份的所有日期

参考