如何在SQL Server中获取最接近记录日期的日期?

时间:2018-12-23 07:14:25

标签: sql sql-server

我具有以下数据结构:

表A

ID  |  RequestNumber  |  Date
----+-----------------+-----------
 1  |      1          | 2017/09/27
 2  |      1          | 2018/06/02

表B

RequestNumber  |  Serial  |  Date
---------------+----------+-----------
     1         |    1     | 2017/09/27
     1         |    2     | 2017/09/27
     1         |    6     | 2018/06/03
     1         |    7     | 2018/06/03
     1         |    8     | 2018/06/03

我们可以看到,Table A中与Table B第一行最接近的日期是2017/09/27,而{{ {1}}

所以...

我需要一个查询,以使Table B中的每一行与Table A中的所有行都与Table B中的记录最接近(这意味着应该向第一条记录返回2条记录并返回3条记录作为第二条记录)

预期结果将是:

Table A

预先感谢

4 个答案:

答案 0 :(得分:1)

此查询将执行您想要的操作。它将TableATableB上的RequestNumber连接,然后连接到DATEDIFFTableB之间的最小TableA值的表中,以确保我们仅获得结果中最接近的日期:

SELECT a.ID, a.RequestNumber, b.Serial, b.Date 
FROM TableA a
JOIN TableB b ON b.RequestNumber = a.RequestNumber
JOIN (SELECT a.ID AS ID, MIN(ABS(DATEDIFF(day, b.Date, a.Date))) AS days
      FROM TableA a
      JOIN TableB b ON b.RequestNumber = a.RequestNumber
      GROUP BY a.ID) c  ON c.ID = a.ID AND c.days = ABS(DATEDIFF(day, b.Date, a.Date))

输出:

ID  RequestNumber   Serial  Date
1   1               1       27/09/2017 09:30:00
1   1               2       27/09/2017 09:30:00
2   1               6       03/06/2018 09:30:00
2   1               7       03/06/2018 09:30:00
2   1               8       03/06/2018 09:30:00

Demo on dbfiddle

答案 1 :(得分:0)

另一种可能的方法是使用LEFT JOIN和DENSE_RANK()。我假设您想要最接近且大于日期的日期:

CREATE TABLE #TableA (
   ID int,
   RequestNumber int,
   [Date] date
)
CREATE TABLE #TableB (
   RequestNumber int,
   Serial int,
   [Date] date
)
INSERT INTO #TableA (ID, RequestNumber, [Date])
VALUES
   (1, 1, '2017-09-27'),
   (2, 1, '2018-06-02')

INSERT INTO #TableB (RequestNumber, Serial, [Date])
VALUES
   (1, 1, '2017-09-27'),
   (1, 2, '2017-09-27'),
   (1, 6, '2018-06-03'),
   (1, 7, '2018-06-03'),
   (1, 8, '2018-06-03'),
   (1, 9, '2018-06-05'),
   (1, 10, '2018-06-07')

; WITH cte AS (
   SELECT 
      a.ID, 
      a.RequestNumber, 
      b.Serial, 
      b.[Date], 
      DENSE_RANK() OVER (PARTITION BY a.ID, a.RequestNumber ORDER BY a.ID, a.RequestNumber, b.[Date]) AS rn
   FROM #TableA a
   LEFT JOIN #TableB b ON (a.RequestNumber = b.RequestNumber) AND (a.[Date] <= b.[Date])
)
SELECT
      ID, 
      RequestNumber, 
      Serial, 
      [Date]
FROM cte
WHERE rn = 1
ORDER BY ID, RequestNumber

输出:

ID  RequestNumber   Serial  Date
1   1   1   27/09/2017 00:00:00
1   1   2   27/09/2017 00:00:00
2   1   6   03/06/2018 00:00:00
2   1   7   03/06/2018 00:00:00
2   1   8   03/06/2018 00:00:00

答案 2 :(得分:0)

这将为您提供所需的,尽管我认为您实际上并未为从所需输出中看到的日期正确指定较小的日期术语。

    Select * from table B
       left join table A
         on 
       B.requestNumber=A.requestNumber
         and B.date >=A.Date;

答案 3 :(得分:0)

这是使用横向联接(apply关键字)的好地方:

select a.*, b.*
from tablea a cross apply
     (select top (1) with ties b.*
      from tableb b
      order by abs(datediff(day, a.date, b.date))
     ) b;

Here是db <>小提琴。