使用内部联接更新 table

Update table with inner join

我有两个 table 如下所示

BidID   CreatedDate              BidVersionNumber
2       2018-05-17 04:35:40.320    AB25443/01-01
3       2018-06-11 03:36:59.977    AB25443/01-01

第二个Table

BidVersionNumber       CreatedDate        BidId
 AB25443/01-01       5/17/2018 4:35:40
 AB25443/01-01       5/17/2018 4:41:10     

我想通过在 BidVersionNumber 上加入第一个 table 来更新第二个 table 的 BidId,但是由于 BidVersionNumber 是相同的,所以我也想在 CreatedDate 上匹配加入 4:35:40 部分时间。谁能告诉我如何加入两个专栏。

期望的输出

BidVersionNumber       CreatedDate        BidId
 AB25443/01-01       5/17/2018 4:35:40     2
 AB25443/01-01       5/17/2018 4:41:10     3  

这里棘手的部分是时间比较。这样做的一种方法是从 t1 时间中减去一秒,基本上使用 between:

update t2
    set bidId = t1.BidId
    from table2 t2 join
         table1 t1
         on t2.BidVersionNumber = t1.BidVersionNumber and
            t2.CreatedDate <= t1.CreatedDate and
            t2.CreatedDate > dateadd(second, -1, t1.CreatedDate);