Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将下一行数据添加到同一表中的新列中_Sql_Sql Server - Fatal编程技术网

Sql 将下一行数据添加到同一表中的新列中

Sql 将下一行数据添加到同一表中的新列中,sql,sql-server,Sql,Sql Server,这是我的桌子: 我想更新Addr=Office的Closed_date和Closed_time列。关闭时间作为下一行更新日期和更新时间 输出应为: 我一直在尝试使用行id使用大小写表达式获取唯一标识: Declare @maxRow int Select @maxrow= max(ID) from Info Declare @ID int=1 While(@ID <= @maxRow) Begin Declare @Name varchar(10), @Addr varchar(1

这是我的桌子:

我想更新Addr=Office的Closed_date和Closed_time列。关闭时间作为下一行更新日期和更新时间

输出应为:

我一直在尝试使用行id使用大小写表达式获取唯一标识:

Declare @maxRow int
Select @maxrow= max(ID) from Info

Declare @ID int=1
While(@ID <= @maxRow)
Begin
   Declare @Name varchar(10), @Addr varchar(10), @Update_date datetime, @Update_time varchar(10), @Closed_date datetime, @Closed_time varchar(10)

   Select @Addr = Case when Inf.Addr = 'Office' then ((Select Update_date from Info where ID=@ID+1) as @Closed_date) else Inf.Name end
我假设您正在使用Sql Server:


请标记使用的数据库管理系统。看起来不像ANSI SQL…它的SQL server 2008@jarlh谢谢Giorgi:它工作了。我试过使用Cte,但我缺少addr的条件。ThanksUpdate\u日期和更新时间应按升序排列,然后我需要更新此查询。
;with cte as(select *, row_number() over(order by ID) rn from Info)

update c1 set Closed_date = c2.Update_date,
              Closed_time = c2.Update_time 
from cte c1
join cte c2 on c1.rn = c2.rn - 1 and c1.Addr = 'Office' and c2.Addr <> 'Office'