我想为特定分钟加入2个表date的值。说数据加入同一日期,但小时数相差+或-10分钟。
select * from table1 inner join table2 where table1.starttime = table2.starttime
在这里,我有10分钟的等待时间,如果差异为+或-10分钟,则必须显示值
答案 0 :(得分:4)
您想要这样的东西吗?
select *
from table1 t1 inner join
table2 t2
on t1.starttime >= dateadd(minute, -10, t2.starttime) or
t1.starttime <= dateadd(minute, 10, t2.starttime);
这不在同一天检查。如果那也是可取的:
select *
from table1 t1 inner join
table2 t2
on ( t1.starttime >= dateadd(minute, -10, t2.starttime) or
t1.starttime <= dateadd(minute, 10, t2.starttime)
) and
convert(date, t1.starttime) = convert(date, t2.starttime);