34,593
社区成员
发帖
与我相关
我的任务
分享
select * from TableName as a where not exists(select 1 from TableName where replace(a.Name,'市','') like replace(Name,'市','')+'%' and len(Name)>len(a.Name))
DELETE A from TableName as a where exists(select 1 from TableName where replace(a.Name,'市','') like replace(Name,'市','')+'%' and len(Name)>len(a.Name))
create table #tab
(
id int,
name varchar(50)
)
insert into #tab
select 1,'深圳市华为科技有限公司' union all
select 2,'深圳华为科技有限公司' union all
select 3,'深圳华为科技公司' union all
select 4,'深圳市华为科技公司'union all
select 7,'深圳华为公司' union all
select 6,'深圳华为' union all
select 5,'北京市大大公司' union all
select 10,'北京大大有限公司' union all
select 0,'北京市大大科技公司'
select id,left(replace(name,'市',''),4),name,
ROW_NUMBER()over(PARTITION BY left(replace(name,'市',''),4) ORDER BY len(name) desc) as rw
from #tab
drop table #tab
/*
(9 行受影响)
id name rw
----------- -------- -------------------------------------------------- --------------------
0 北京大大 北京市大大科技公司 1
10 北京大大 北京大大有限公司 2
5 北京大大 北京市大大公司 3
1 深圳华为 深圳市华为科技有限公司 1
2 深圳华为 深圳华为科技有限公司 2
4 深圳华为 深圳市华为科技公司 3
3 深圳华为 深圳华为科技公司 4
7 深圳华为 深圳华为公司 5
6 深圳华为 深圳华为 6
(9 行受影响)
*/
--把rw是1的保留,其他的删掉.
declare @str varchar(30)
set @str ='深圳市华为科技有限公司'
select left(replace(@str,'市',''),4)
/*
--------
深圳华为
(1 行受影响)
*/
可以试一下,把‘省市区县‘等字去掉,然后取前4个或者5个字段进行分组排序,然后加上序号,按您自己的规则删除(是保留最长的,还是保留最短的)。
但,这只是一个参考。