求区间内最近的时间

好人吗 2011-11-20 04:53:12

create table t(id date);
insert into t
select '2010-9-3'
union all
select '2010-9-1'
union all
select '2010-9-5'
union all
select '2010-9-22';

求距离 '2010-9-1'最近的时间。


小弟第一次发帖,也不知道自己有多少分,求高手解决。
...全文
86 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
好人吗 2011-11-21
  • 打赏
  • 举报
回复


select a.ID,b.ID as 最近
from t as a
Cross apply
(select *,DENSE=DENSE_RANK()over(order by abs(DATEDIFF(D,a.ID,ID)) ) from t where ID<>a.ID ) as b
where b.DENSE=1
order by 1
/*
解释:1.cross apply是sql server 2005之后增加的一个关键词,前身是corss join。
2.dense_rank是一个结果集排序函数。
3.通过把一个表与一个(假)函数链接,避免了使用游标。(我就是不想使用游标故有此一问)
4.sql的排序允许对排序列进行计算。

*/

结贴。
好人吗 2011-11-20
  • 打赏
  • 举报
回复
我想把这个问题再抽象点,呵呵……
好人吗 2011-11-20
  • 打赏
  • 举报
回复
roy_88老师的回答已经解决了该问题。我这个问题是我看了别人的一个帖子,感觉别人的回答有问题,但是我又无法用像roy_88老师这样简洁的语言来解决,我是sql菜鸟,qianjin036a老师的代码和roy_88老师的代码捣鼓了3个小时才大概弄明白,等我完全弄清楚了再结贴。
在此谢谢各位老师的热心解答。
pengxuan 2011-11-20
  • 打赏
  • 举报
回复

create table t(id datetime)
insert into t
select '2010-9-3'
union all
select '2010-9-1'
union all
select '2010-9-5'
union all
select '2010-9-22'
go
select * from (
select top 1 * from t where id<'2010-9-3' order by id) a
union all
select * from (
select top 1 * from t where id>'2010-9-3' order by id) b
/*
id
-----------------------
2010-09-01 00:00:00.000
2010-09-05 00:00:00.000

(2 行受影响)
*/
中国风 2011-11-20
  • 打赏
  • 举报
回复
create table t(id date);
insert into t
select '2010-9-3'
union all
select '2010-9-1'
union all
select '2010-9-5'
union all
select '2010-9-22'
go

drop table t

select a.ID,b.ID as 最近
from t as a
Cross apply
(select *,DENSE=DENSE_RANK()over(order by abs(DATEDIFF(D,a.ID,ID)) ) from t where ID<>a.ID ) as b
where b.DENSE=1
order by 1
/*
ID 最近
2010-09-01 2010-09-03
2010-09-03 2010-09-01
2010-09-03 2010-09-05
2010-09-05 2010-09-03
2010-09-22 2010-09-05
*/
-晴天 2011-11-20
  • 打赏
  • 举报
回复
create table t(id datetime);
insert into t
select '2010-9-3'
union all
select '2010-9-1'
union all
select '2010-9-5'
union all
select '2010-9-22';
go
select id,(select top 1 id from t where id<>a.id order by abs(datediff(d,id,a.id)))as latestid
from t a
/*
id latestid
----------------------- -----------------------
2010-09-03 00:00:00.000 2010-09-05 00:00:00.000
2010-09-01 00:00:00.000 2010-09-03 00:00:00.000
2010-09-05 00:00:00.000 2010-09-03 00:00:00.000
2010-09-22 00:00:00.000 2010-09-05 00:00:00.000

(4 行受影响)

*/
go
drop table t
-晴天 2011-11-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 down_login 的回复:]
我的意思是存在一个离散的时间区间,例如 '2010-9-1'到 '2010-9-22',现有时间数据 '2010-9-1'
'2010-9-3','2010-9-5','2010-9-22'。在这些已知的时间内,求距离各个时间最近的时间,例如距离 '2010-9-1'最近的是 '2010-9-3',距离相差两天,而距离 '2010-9-3'最近的是 '2010-9-1'和'2010-9-5',……

还麻烦能解释下order by abs(datediff(d,id,@dt))这句吗?
[/Quote]
我以为你是要指定一个日期的,那就是以这个日期为标准,拿其他日期与该日期的天数相减,得到的结果取绝对值,那最小的,就用 top 1 取出来.
好人吗 2011-11-20
  • 打赏
  • 举报
回复
我的意思是存在一个离散的时间区间,例如 '2010-9-1'到 '2010-9-22',现有时间数据 '2010-9-1'
'2010-9-3','2010-9-5','2010-9-22'。在这些已知的时间内,求距离各个时间最近的时间,例如距离 '2010-9-1'最近的是 '2010-9-3',距离相差两天,而距离 '2010-9-3'最近的是 '2010-9-1'和'2010-9-5',距离都是相差两天,距离 '2010-9-22'最近的是 '2010-9-5'了。
还麻烦能解释下order by abs(datediff(d,id,@dt))这句吗?
--小F-- 2011-11-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qianjin036a 的回复:]
SQL code
create table t(id datetime);
insert into t
select '2010-9-3'
union all
select '2010-9-1'
union all
select '2010-9-5'
union all
select '2010-9-22';
go
declare @dt datetime
set @dt=……
[/Quote]
恩 也许就是这个意思。
--小F-- 2011-11-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 down_login 的回复:]
不好意思,错了,应该是求距离 '2010-9-3'最近的时间。语句该怎么写?
[/Quote]
最近的时间不就是 2010-09-03么?
-晴天 2011-11-20
  • 打赏
  • 举报
回复
create table t(id datetime);
insert into t
select '2010-9-3'
union all
select '2010-9-1'
union all
select '2010-9-5'
union all
select '2010-9-22';
go
declare @dt datetime
set @dt='2010-9-3'
select top 1 * from t
where id<>@dt
order by abs(datediff(d,id,@dt))
/*
id
-----------------------
2010-09-05 00:00:00.000

(1 行受影响)
*/

go
drop table t
--小F-- 2011-11-20
  • 打赏
  • 举报
回复
select top 1 * from t order by abs(datediff(dd,id,'2010-09-03'))
好人吗 2011-11-20
  • 打赏
  • 举报
回复
不好意思,错了,应该是求距离 '2010-9-3'最近的时间。语句该怎么写?

34,593

社区成员

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

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