美好的一天。我在数据库中有两个表。我想加入他们,但我似乎检索的行数超过了所需的数量。除了日期范围之外,两个表都没有任何关系。
如果表b的日期在表a的日期之内;然后加入。但是,我似乎加入了许多不必要的方式。
如何附上表B中的用户名,即表A中最近的日期范围?任何帮助都非常感谢。
表A
dateS auditmessage
2018-04-09 07:06:48.290 Phase 2) (Heat Up) Parameter 3) changed from 8.0 to 5.0
2018-04-09 07:06:48.290 Phase 2) (Heat Up) Parameter 24) changed from 15.0 to 14.5
2018-04-09 07:06:48.290 Phase 3) (Sterilization) Parameter 24) changed from 15.0 to 14.5
2018-04-09 07:06:48.290 Phase 4) (Air Pressurization) Parameter 11) changed from 15.0 to 14.5
2018-04-09 07:06:51.380 Phase 2) (Heat Up) Parameter 3) changed from 8.0 to 5.0
2018-04-09 07:06:51.380 Phase 2) (Heat Up) Parameter 24) changed from 15.0 to 14.5
2018-04-09 07:06:51.380 Phase 3) (Sterilization) Parameter 24) changed from 15.0 to 14.5
2018-04-09 07:06:51.380 Phase 4) (Air Pressurization) Parameter 11) changed from 15.0 to 14.5
2018-04-09 07:08:31.737 Phase 1) (Setup) Parameter 18) changed from 6.0 to 7.0
2018-04-09 07:08:31.737 Phase 4) (Air Pressurization) Parameter 20) changed from 0 to 1
2018-04-09 07:08:34.977 Phase 1) (Setup) Parameter 18) changed from 6.0 to 7.0
2018-04-09 07:08:34.977 Phase 4) (Air Pressurization) Parameter 20) changed from 0 to 1
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 1) changed from 0 to 2
2018-04-09 09:25:15.967 Phase 4) (Air Pressurization) Parameter 11) changed from 14.5 to 17.5
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 18) changed from 6.0 to 5.0
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 3) changed from 2 to 0
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 18) changed from 7.0 to 6.0
2018-04-09 09:25:15.967 Phase 2) (Heat Up) Parameter 24) changed from 14.5 to 17.5
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 1) changed from 0 to 2
2018-04-09 09:25:19.247 Phase 4) (Air Pressurization) Parameter 11) changed from 14.5 to 17.5
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 18) changed from 6.0 to 5.0
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 3) changed from 2 to 0
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 18) changed from 7.0 to 6.0
2018-04-09 09:25:19.247 Phase 2) (Heat Up) Parameter 24) changed from 14.5 to 17.5
2018-04-09 12:32:30.123 Phase 1) (Setup) Parameter 3) changed from 0 to 2
2018-04-09 12:32:32.253 Phase 1) (Setup) Parameter 3) changed from 0 to 2
表B
userName DateS
sam 2018-04-09 07:11:45.907
andy 2018-04-09 09:16:41.023
carl 2018-04-09 12:17:45.057
我的查询失败
SELECT
k.dates,
k.auditmessage,
l.username
FROM tableA k
INNER JOIN tableB l
-- on l.dateS >=k.dateS --and l.dateS <= k.dateS
--where l.dateS >=k.dateS and l.dateS<=k.dateS
期望的结果
dateS auditmessage User
2018-04-09 07:06:48.290 Phase 2) (Heat Up) Parameter 3) changed from 8.0 to 5.0 sam
2018-04-09 07:06:48.290 Phase 2) (Heat Up) Parameter 24) changed from 15.0 to 14.5 sam
2018-04-09 07:06:48.290 Phase 3) (Sterilization) Parameter 24) changed from 15.0 to 14.5 sam
2018-04-09 07:06:48.290 Phase 4) (Air Pressurization) Parameter 11) changed from 15.0 to 14.5 sam
2018-04-09 07:06:51.380 Phase 2) (Heat Up) Parameter 3) changed from 8.0 to 5.0 sam
2018-04-09 07:06:51.380 Phase 2) (Heat Up) Parameter 24) changed from 15.0 to 14.5 sam
2018-04-09 07:06:51.380 Phase 3) (Sterilization) Parameter 24) changed from 15.0 to 14.5 sam
2018-04-09 07:06:51.380 Phase 4) (Air Pressurization) Parameter 11) changed from 15.0 to 14.5 sam
2018-04-09 07:08:31.737 Phase 1) (Setup) Parameter 18) changed from 6.0 to 7.0 sam
2018-04-09 07:08:31.737 Phase 4) (Air Pressurization) Parameter 20) changed from 0 to 1 sam
2018-04-09 07:08:34.977 Phase 1) (Setup) Parameter 18) changed from 6.0 to 7.0 sam
2018-04-09 07:08:34.977 Phase 4) (Air Pressurization) Parameter 20) changed from 0 to 1 sam
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 1) changed from 0 to 2 andy
2018-04-09 09:25:15.967 Phase 4) (Air Pressurization) Parameter 11) changed from 14.5 to 17.5 andy
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 18) changed from 6.0 to 5.0 andy
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 3) changed from 2 to 0 andy
2018-04-09 09:25:15.967 Phase 1) (Setup) Parameter 18) changed from 7.0 to 6.0 andy
2018-04-09 09:25:15.967 Phase 2) (Heat Up) Parameter 24) changed from 14.5 to 17.5 andy
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 1) changed from 0 to 2 andy
2018-04-09 09:25:19.247 Phase 4) (Air Pressurization) Parameter 11) changed from 14.5 to 17.5 andy
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 18) changed from 6.0 to 5.0 andy
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 3) changed from 2 to 0 andy
2018-04-09 09:25:19.247 Phase 1) (Setup) Parameter 18) changed from 7.0 to 6.0 andy
2018-04-09 09:25:19.247 Phase 2) (Heat Up) Parameter 24) changed from 14.5 to 17.5 andy
2018-04-09 12:32:30.123 Phase 1) (Setup) Parameter 3) changed from 0 to 2 carl
2018-04-09 12:32:32.253 Phase 1) (Setup) Parameter 3) changed from 0 to 2 carl
答案 0 :(得分:2)
您可以使用outer apply
搜索最近的行。对于TableB中的每一行,它根据距离对TableA行进行排序,然后选择第一行:
select *
from TableB b
outer apply
(
select top 1 *
from TableA a
order by
abs(b.dateS - a.dateS)
) a