关于自表更新的问题

律己修心 2008-03-27 05:57:59
有这样一个表
I S E
5 2008/02/02 2008/05/02
5 2008/03/02 2008/05/02
5 2008/04/02 null

现想要更新这个表
在I值相同的条件下
用E值段来更新S值段

更新后的结果应该是这样的
I S E
5 2008/02/02 2008/03/02
5 2008/03/02 2008/04/02
5 2008/04/02 null

S值段值最大的不被更新

请问应该怎样实现?
谢谢

...全文
63 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
青锋-SS 2008-03-27
  • 打赏
  • 举报
回复
create table tb(I int,S datetime,E datetime)
go
insert into tb select 5, '2008/02/02', '2008/05/02'
insert into tb select 5, '2008/03/02', '2008/05/02'
insert into tb select 5, '2008/04/02', null
go
select i,convert(char(10),s,120) as s,convert(char(10),e,120) as e from tb
update tb
set e=(select min(s) from tb a where a.i=tb.i and a.s>tb.s)
select i,convert(char(10),s,120) as s,convert(char(10),e,120) as e from tb
go
drop table tb
go



i s e
----------- ---------- ----------
5 2008-02-02 2008-05-02
5 2008-03-02 2008-05-02
5 2008-04-02 NULL

(3 行受影响)

(3 行受影响)

i s e
----------- ---------- ----------
5 2008-02-02 2008-03-02
5 2008-03-02 2008-04-02
5 2008-04-02 NULL

(3 行受影响)

青锋-SS 2008-03-27
  • 打赏
  • 举报
回复
update tb
set e=(select min(s) from tb a where a.i=tb.i and a.s>tb.s)

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧