◆◆请问这样的该如何写(筛选,填入)

guzh 2003-08-21 11:52:35
表名:test
name dt    pay
aa  2003-1-1  1
bb  2003-1-2  1
cc  2003-1-3  1
aa  2003-2-1  1
bb  2003-3-1  1
cc  2003-4-1  1
aa        0
bb        0
ff        0

从pay=1中取出最近日期的name,然后将填入到名字相同的pay=0中,如果pay=0但pay=1中没有相同名字的就用getdate()填充.

从上表我想得到的结果是
name dt    
aa  2003-2-1
bb  2003-3-1
ff  <getdate()>

cc就不出现了.

请问该如何写,谢谢!!!
...全文
25 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
愉快的登山者 2003-08-21
  • 打赏
  • 举报
回复
update test set test.dt = A.dt from
(select name, isnull(max(dt), getdate()) dt from test group by name) A
where test.name = A.name and test.pay = 0

愉快的登山者


◢◣◢◣◢◣
CrazyFor 2003-08-21
  • 打赏
  • 举报
回复
update test set
dt=case when a.dt is not null then a.dt else getdate() end
from (select * from test aa where not exists (select 1 from test bb where aa.name=bb.name and aa.dt<bb.dt and bb.dt is not null) and aa.dt is not null) a
where test.name=a.name and aa.pay=0
pengdali 2003-08-21
  • 打赏
  • 举报
回复
update test set dt=isnull((select max(tem.dt) from test tem where tem.pay=1 and tem.name=test.name),getdate()) where pay=0

34,576

社区成员

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

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