id Bus arrival time
1 6:30
2 7:00
3 9:00
4 10:00
5 15:00
6 16:00
如果一辆公共汽车在6:40到达,那么最近的时间应该是6:30
如果公交车在9:35到达,最近的时间应该是10:00,我需要解决方案作为T-SQL查询
先谢谢, 阿尼尔
答案 0 :(得分:4)
declare @checkTime time = '6:40'
select top 1 * from schedule
order by ABS(DATEDIFF(Second, @checkTime, busArrivalTime))
答案 1 :(得分:0)
你可以这样做:
DECLARE @TIME TIME = '6:45'
DECLARE @TEMP TABLE(ID int,start time);
insert into @temp values(1, '6:30')
insert into @temp values(1, '7:00')
insert into @temp values(1, '9:00')
insert into @temp values(1, '10:00')
insert into @temp values(1, '15:00')
insert into @temp values(1, '6:30')
insert into @temp values(1, '16:00')
SELECT top(1)start ,ABS(DATEDIFF(MINUTE , @TIME ,START )) as diff
FROM @TEMP
order by diff