客户经理服务对照表T (大约5W行);
CUS_ID,CM_NAME,CUS_MOBILE
101,李强,1311234567
101,周倩,1321234567
101,张三,1331234567
102,NULL,1891234567
102,李四,1381234567
103,王五,1380000000
103,NULL, 1361234567
104,郭晓,1341234561
104,张雨,1321456123
---需要得到的结果如下:
CUS_ID,CM_NAME,CUS_MOBILE
101,李强,1311234567
101,李强,1321234567
101,李强,1331234567
102,李四,1891234567
102,李四,1381234567
103,王五,1380000000
103,王五, 1361234567
104,郭晓,1341234561
104,郭晓,1321456123
#业务是这样理解的:
每个业务经理CM_NAME,必须服务一批CUS_ID,但是又不能多个客户经理服务同一个CUS_ID,所以就有
了这个问题。
遵循原则:
1.如果一个CUS_ID原来有多个客户经理服务,那么就随机取一个,取第一个也行,如:101
2.如果一个CUS_ID新增加一个手机号码,但是人还是一个嘛,所以CM_NAME要保持原来的,如:102
3.技术要求直接更新表T就行,不用写临时表
谢谢各位支招!!!
------解决方案--------------------
;with cte as(
select row_number() over (partition by CUS_ID order by getdate()) m,* from #a
)
select CUS_ID, (select top 1 CM_NAME from cte where CUS_ID=a.CUS_ID and isnull(CM_NAME,'')!='' order by m )CM_NAME
,CUS_MOBILE from cte a order by CUS_ID
----查询结果
(9 行受影响)
CUS_ID CM_NAME CUS_MOBILE
----------- -------------------------------------------------- -----------
101 李强 1311234567
101 李强 1321234567
101 李强 1331234567
102 李四 1891234567
102 李四 1381234567
103 王五 1380000000
103 王五 1361234567
104 郭晓 1341234561
104 郭晓 1321456123
------解决方案--------------------
create table 客户经理服务对照表
(CUS_ID int,CM_NAME varchar(10),CUS_MOBILE varchar(20))
insert into 客户经理服务对照表
select 101,'李强','1311234567' union all
select 101,'周倩','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' union all
select 104,'郭晓','1341234561' union all
select 104,'张雨','1321456123'
update t