表名T(大约5W行);
CUS_ID,CUS_NAME,CUS_MOBILE
101,NULL,1311234567
101,NULL,1321234567
101,张三,1331234567
102,NULL,1891234567
102,李四,1381234567
103,王五,1380000000
103,NULL, 1361234567
---需要得到的结果如下:
CUS_ID,CUS_NAME,CUS_MOBILE
101,张三,1311234567
101,张三,1321234567
101,张三,1331234567
102,李四,1891234567
102,李四,1381234567
103,王五,1380000000
103,王五, 1361234567
--如上图的红色字体一样,把NULL值更新为CUS_NAME,谢谢各位支招!!!
------解决方案--------------------
这样吗:
--drop table t
create table t(CUS_ID int,CUS_NAME varchar(10),CUS_MOBILE varchar(20))
insert into t
select 101,NULL,'1311234567' union all
select 101,NULL,'1321234567' union all
select 101,'张三','1331234567' union all
select 102,NULL,'1891234567' union all
select 102,'李四','1381234567' union all
select 103,'王五','1380000000' union all
select 103,NULL,'1361234567'
go
update t
set CUS_NAME = tt.CUS_NAME
from t
inner join t tt
on t.CUS_ID = tt.CUS_ID
where t.CUS_NAME is null and tt.CUS_NAME is not null
select * from t
/*
CUS_ID CUS_NAME CUS_MOBILE
101 张三 1311234567
101 张三 1321234567
101 张三 1331234567
102 李四 1891234567
102 李四 1381234567
103 王五 1380000000
103 王五 1361234567
*/