如果日期无效,SQL将加入下一个可用日期

时间:2018-04-11 10:08:43

标签: sql join sql-server-2016

寻求查询方面的帮助。我有一个客户表记录客户链接到基本位置。以及详细说明特定时间点位置地址的位置表。

CREATE TABLE [dbo].[Customer](
   [ID] [int] NULL,
   [Date of Transfer] [datetime] NULL,
   [Old Location] [int] NULL,
   [New Location] [int] NULL)


INSERT INTO [dbo].[Customer] (ID, [Date of Transfer], [Old Location], [New Location])
VALUES  (1, '2016-07-01 00:00:00.000', 1001, 2200),
        (1, '2017-11-25 00:00:00.000', 2200, 3078),
        (1, '2018-01-01 00:00:00.000', 3078, 8500),
        (2, '2016-02-27 00:00:00.000', 5578, 9901),
        (2, '2017-06-22 00:00:00.000', 9901, 9001);

CREATE TABLE [dbo].[Location](
    [Location Code] [int] NULL,
    [Location Address] [nvarchar](50) NULL,
    [Date From] [date] NULL,
    [Date To] [date] NULL)


INSERT INTO [dbo].[Location] ([Location Code], [Location Address], [Date From], [Date To])
VALUES (1001, 'Logan Street', '1998-02-01 00:00:00.000', '2017-07-21 00:00:00.000'),
    (2200, 'Ashdown Avenue', '2014-01-31 00:00:00.000', NULL),
    (3078, 'Chessington Court', '2009-05-11 00:00:00.000', '2018-03-30 00:00:00.000'),
    (8500, 'Cambridge Green', '2018-01-15 00:00:00.000', '2018-02-15 00:00:00.000'),
    (8500, 'Blair Road', '2018-02-15 00:00:00.000', NULL),
    (5578, 'Aberdeen View', '2000-11-19 00:00:00.000', '2017-05-13 00:00:00.000'),
    (9901, 'Tay Road', '2013-02-02 00:00:00.000', '2018-01-22 00:00:00.000'),
    (9001, 'Wellington Way', '2015-02-16 00:00:00.000', '2018-02-22 00:00:00.000');

我试图加入两个表格,以便向我提供与客户相关联的位置的地址,这是他们转移到另一个位置的点。对于大多数人来说,使用以下

可以正常工作
SELECT [ID]
  ,[Date of Transfer]
  ,[Old Location]
  ,O.[Location Address]
  ,O.[Date From]
  ,O.[Date To]
  ,[New Location]
  ,N.[Location Address]
  ,N.[Date From]
  ,N.[Date To]
FROM [dbo].[Customer]
Left Join [dbo].[Location] O on [Old Location] = O.[Location Code] and ([Date of Transfer] >= O.[Date From] OR O.[Date From] is NULL)  and ([Date of Transfer] <= O.[Date To] or O.[Date To] is NULL)
Left Join [dbo].[Location] N on [New Location] = N.[Location Code] and ([Date of Transfer] >= N.[Date From] OR N.[Date From] is NULL)  and ([Date of Transfer] <= N.[Date To] or N.[Date To] is NULL)
ORDER BY [ID], [Date of Transfer] ASC

我遇到的问题是位置表有时会延迟更新,因此客户转移不会加入任何有效位置。我想添加一个检查位置有效日期的子句。因此,在上面的例子中,我想将客户1的转账链接转移到8500转到转移后1个月左右更新的剑桥绿地址。有谁知道我怎么能做到这一点。

当前示例输出

ID          Date of Transfer        Old Location Location Address                                   Date From  Date To    New Location Location Address                                   Date From  Date To
----------- ----------------------- ------------ -------------------------------------------------- ---------- ---------- ------------ -------------------------------------------------- ---------- ----------
1           2016-07-01 00:00:00.000 1001         Logan Street                                       1998-02-01 2017-07-21 2200         Ashdown Avenue                                     2014-01-31 NULL
1           2017-11-25 00:00:00.000 2200         Ashdown Avenue                                     2014-01-31 NULL       3078         Chessington Court                                  2009-05-11 2018-03-30
1           2018-01-01 00:00:00.000 3078         Chessington Court                                  2009-05-11 2018-03-30 8500         NULL                                               NULL       NULL
2           2016-02-27 00:00:00.000 5578         Aberdeen View                                      2000-11-19 2017-05-13 9901         Tay Road                                           2013-02-02 2018-01-22
2           2017-06-22 00:00:00.000 9901         Tay Road                                           2013-02-02 2018-01-22 9001         Wellington Way                                     2015-02-16 2018-02-22

客户1的最后一行尚未链接到某个位置,因为在转移期间没有列出该期间的位置。因此,我希望检查自转移日期以来该位置代码的下一个位置地址。

注意:位置表每个位置可以有多个地址(在不同的时间段内。因此,我不能使用简单的连接。

期望的输出。

ID          Date of Transfer        Old Location Location Address                                   Date From  Date To    New Location Location Address                                   Date From  Date To
----------- ----------------------- ------------ -------------------------------------------------- ---------- ---------- ------------ -------------------------------------------------- ---------- ----------
1           2016-07-01 00:00:00.000 1001         Logan Street                                       1998-02-01 2017-07-21 2200         Ashdown Avenue                                     2014-01-31 NULL
1           2017-11-25 00:00:00.000 2200         Ashdown Avenue                                     2014-01-31 NULL       3078         Chessington Court                                  2009-05-11 2018-03-30
1           2018-01-01 00:00:00.000 3078         Chessington Court                                  2009-05-11 2018-03-30 8500         Cambridge Green                                    2018-01-15 2018-02-15
2           2016-02-27 00:00:00.000 5578         Aberdeen View                                      2000-11-19 2017-05-13 9901         Tay Road                                           2013-02-02 2018-01-22
2           2017-06-22 00:00:00.000 9901         Tay Road                                           2013-02-02 2018-01-22 9001         Wellington Way                                     2015-02-16 2018-02-22

2 个答案:

答案 0 :(得分:1)

试试这个。

SELECT [ID]
  ,[Date of Transfer]
  ,[Old Location]
  ,O.[Location Address]
  ,O.[Date From]
  ,O.[Date To]
  ,[New Location]
  ,N.[Location Address]
  ,N.[Date From]
  ,N.[Date To]
FROM [dbo].[Customer]
Left Join [dbo].[Location] O on [Old Location] = O.[Location Code] and ([Date of Transfer] >= O.[Date From] OR O.[Date From] is NULL)  and ([Date of Transfer] <= O.[Date To] or O.[Date To] is NULL)
Left Join [dbo].[Location] N on [New Location] = N.[Location Code] and ([Date of Transfer] >= N.[Date From] OR N.[Date From] is NULL or N.[Date To] is NULL)  and ([Date of Transfer] <= N.[Date To] or N.[Date To] is NULL)
ORDER BY [ID], [Date of Transfer] ASC

答案 1 :(得分:0)

试试这个。

SELECT [ID]
  ,[Date of Transfer]
  ,[Old Location]
  ,O.[Location Address]
  ,O.[Date From]
  ,O.[Date To]
  ,[New Location]
  ,N.[Location Address]
  ,N.[Date From]
  ,N.[Date To]
FROM [dbo].[Customer]
Left Join [dbo].[Location] O on [Old Location] = O.[Location Code] and ([Date of Transfer] >= O.[Date From] OR O.[Date From] is NULL)  and ([Date of Transfer] <= O.[Date To] or O.[Date To] is NULL)
Join [dbo].[Location] N on [New Location] = N.[Location Code] and ([Date of Transfer] >= N.[Date From] OR N.[Date From] is NULL)  and ([Date of Transfer] <= N.[Date To] or N.[Date To] is NULL)
ORDER BY [ID], [Date of Transfer] ASC