【问题标题】:Update table based on Dates in SQL Server?根据 SQL Server 中的日期更新表?
【发布时间】:2016-10-17 12:47:01
【问题描述】:

我得到了以下 2 张桌子:

if object_id('tempdb..#t1') is not null 
    drop table #t1

create table #t1 
(
     ID int, 
     opendate datetime, 
     closedate datetime,
     [ADDRESS] varchar(50)
)

insert into #t1 (ID, opendate, closedate)
values (111, '1930-05-01 00:00:00.000', '2004-10-23 00:00:00.000'),
       (111, '2004-10-23 00:00:00.000', '2006-03-26 00:00:00.000'),
       (111, '2006-10-23 00:00:00.000', '2009-03-26 00:00:00.000'),
       (111, '2009-03-26 00:00:00.000', '2013-05-21 00:00:00.000'),
       (111, '2013-05-21 00:00:00.000', '2013-06-18 00:00:00.000'),
       (111, '2013-06-18 00:00:00.000', '2016-04-11 00:00:00.000'),
       (111, '2016-04-11 00:00:00.000', '2016-06-16 00:00:00.000'),
       (111, '2016-06-16 00:00:00.000', '2016-06-21 00:00:00.000'),
       (111, '2016-06-21 00:00:00.000', NULL)

select 
    *
from 
    #t1

if object_id('tempdb..#t2') is not null 
    drop table #t2

create table #t2 
(
    ID int, 
    opendate datetime,
    closedate datetime,
    [ADDRESS] varchar(50) 
)

insert into #t2 (ID, opendate, closedate, [ADDRESS])
values  
 (111,'1930-05-01 00:00:00.000','2004-10-23 00:00:00.000','1 AVENUE'    )
,(111,'2004-10-23 00:00:00.000','2009-03-26 00:00:00.000','2 AVENUE'    )
,(111,'2009-03-26 00:00:00.000','2013-05-21 00:00:00.000','3 AVENUE'    )
,(111,'2013-05-21 00:00:00.000' ,NULL                     ,'5 AVENUE' )
,(111,'2016-04-11 00:00:00.000' ,'2016-06-16 00:00:00.000','6 AVENUE'   )
,(111,'2016-06-16 00:00:00.000' ,NULL                     ,'7 AVENUE'   )
,(111,'2016-06-21 00:00:00.000' ,NULL                     ,'8 AVENUE'   )


select 
    *
from 
    #t2

我想像下面这样更新第一个表:

111 1930-05-01 00:00:00.000 2004-10-23 00:00:00.000 '1 AVENUE'
111 2004-10-23 00:00:00.000 2006-03-26 00:00:00.000 '2 AVENUE'
111 2006-03-26 00:00:00.000 2009-03-26 00:00:00.000 '2 AVENUE'
111 2009-03-26 00:00:00.000 2013-05-21 00:00:00.000 '3 AVENUE'
111 2013-05-21 00:00:00.000 2013-06-18 00:00:00.000 '5 AVENUE'
111 2013-06-18 00:00:00.000 2016-04-11 00:00:00.000 '5 AVENUE'
111 2016-04-11 00:00:00.000 2016-06-16 00:00:00.000 '6 AVENUE'
111 2016-06-16 00:00:00.000 2016-06-21 00:00:00.000 '7 AVENUE'
111 2016-06-21 00:00:00.000 NULL                    '8 AVENUE'          

我尝试了一些方法,但由于空值,它没有返回正确的结果。

谢谢。

【问题讨论】:

  • 不清楚你想完成什么。
  • 我想根据 #t2 表 ID 和打开日期和关闭日期更新 #t1 表地址字段。
  • 让我这样解释一下,如果#t1 opendate 在#t2 打开和关闭日期之间,那么它应该更新地址字段。谢谢。
  • 请阅读this,了解一些改进问题的技巧。

标签: sql-server tsql sql-server-2012


【解决方案1】:
update t1 set address = tmp.address
from (select t1.ID, t1.opendate, ROW_NUMBER() over (partition by t1.opendate order by t2.opendate desc) row, t2.ADDRESS
    from #t1 t1
    inner join #t2 t2 on t1.ID = t2.ID and t1.opendate between t2.opendate and isnull(t2.closedate, t1.opendate)) tmp
inner join #t1 t1 on t1.ID = tmp.ID and t1.opendate = tmp.opendate and tmp.row = 1

【讨论】:

  • 感谢@Viktor Čajbík,它工作正常。你是个了不起的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2018-09-23
  • 2013-05-04
  • 1970-01-01
相关资源
最近更新 更多