我需要以下查询的帮助。根据同一表中的字段更新oldID字段
create table #table1
(id int not null primary key identity,
customer_name varchar(25),
usage float,
oldID int null
)
insert into #table1 values('ABC',46.5,null)
insert into #table1 values('ABC',46.5,null)
insert into #table1 values('DEF',36.8,null)
insert into #table1 values('XYZ',50.1,null)
insert into #table1 values('DEF',36.8,null)
insert into #table1 values('XYZ',50.1,null)
select * from #table1
我希望我的表进行更新,这样
id customer_name usage oldID
----------- ------------------------- ---------------------- -----------
1 ABC 46.5 NULL
2 ABC 46.5 1
3 DEF 36.8 NULL
4 XYZ 50.1 NULL
5 DEF 36.8 3
6 XYZ 50.1 4
- 两个记录具有相同的名称和使用率就是后来的纪录被更新。
- 在新记录中,oldID字段应指向其旧记录(ID)。
虽然在我的实际表中,我有一堆日期字段,我可能可以使用,但这会帮助我现在。
+1的样本数据准备测试。 – danihp