最近的日期在 SQL

Closest Date in SQL

我正在尝试在 SQL Server 2012

中查找 Table_ATable_B 之间的最近日期或实际日期

Table_A

Date
-------
2017-07-15 00:00:00
2017-07-27 00:00:00
2017-07-23 00:00:00

Table_B

DT
------
2017-07-17 00:00:00
2017-07-19 00:00:00
2017-07-23 00:00:00
2017-07-28 00:00:00

条件:

期望的输出:

Date
-----
2017-07-17 00:00:00
2017-07-23 00:00:00
2017-07-28 00:00:00

任何帮助或指导?

使用CROSS APPLYTOP:

SELECT 
    Date = x.DT
FROM Table_A a
CROSS APPLY(
    SELECT TOP(1) DT
    FROM Table_B b
    WHERE b.DT >= a.Date
    ORDER BY b.DT
) x
ORDER BY x.DT