27,581
社区成员




insert into @t values(8,'test3','44444'); --新增一条记录
DECLARE @t table (
id int,
name varchar(10),
tel varchar(10)
)
insert into @t values(1,'zhang','11111');
insert into @t values(2,'zhang','11111');
insert into @t values(3,'zhang','22222');
insert into @t values(4,'test','33333');
insert into @t values(5,'test','12345');
insert into @t values(6,'test1','55555');
insert into @t values(7,'test3','33333');
insert into @t values(8,'test3','44444'); --新增一条记录
;WITH a
AS
(
SELECT
a.*,b.ID AS ID2
FROM @t AS a
INNER JOIN @t AS b ON (a.name = b.name or a.tel = b.tel) AND a.ID<>b.ID
),b
AS
(
SELECT id,NAME,tel FROM a AS a2 WHERE NOT EXISTS(SELECT 1 FROM a WHERE (a.name = a2.name or a.tel = a2.tel) AND a.ID2>a2.ID)
)
SELECT * FROM b
UNION
SELECT * FROM @t AS a2 WHERE NOT EXISTS(SELECT 1 FROM a WHERE id=a2.ID)
/*
id NAME tel
3 zhang 22222
6 test1 55555
8 test3 44444
*/
DECLARE @t table (
id int,
name varchar(10),
tel varchar(10)
)
insert into @t values(1,'zhang','11111');
insert into @t values(2,'zhang','11111');
insert into @t values(3,'zhang','22222');
insert into @t values(4,'test','33333');
insert into @t values(5,'test','12345');
insert into @t values(6,'test1','55555');
insert into @t values(7,'test3','33333');
;WITH a
AS
(
SELECT
a.*,b.ID AS ID2
FROM @t AS a
INNER JOIN @t AS b ON (a.name = b.name or a.tel = b.tel) AND a.ID<>b.ID
),b
AS
(
SELECT id,NAME,tel FROM a AS a2 WHERE NOT EXISTS(SELECT 1 FROM a WHERE (a.name = a2.name or a.tel = a2.tel) AND a.ID2>a2.ID)
)
SELECT * FROM b
UNION
SELECT * FROM @t AS a2 WHERE NOT EXISTS(SELECT 1 FROM a WHERE id=a2.ID)
/*
id NAME tel
3 zhang 22222
6 test1 55555
7 test3 33333
*/
with nameg as(
--name group
select tcount.*,ng.namegroup from
(
select t.id,t.name,t.tel,row_number() over (partition by name order by id desc) as namenum
from t) as tcount
join
(
select distinct name,row_number() over (order by name) as namegroup
from t
group by name
) as ng on tcount.name=ng.name
),
telg as(
--tel group
select telcount.*,ng.telgroup from
(
select id,name,tel,row_number() over (partition by tel order by id desc) as telnum
from t
) as telcount
join
(
select distinct tel,row_number() over (order by tel) as telgroup
from t
group by tel
) as ng on telcount.tel=ng.tel
),
mergeG as
(
--merge
select nameg.*,telg.telnum,telg.telgroup from
nameg
join telg on nameg.id=telg.id
)
--select max(id) as gid from
--(
--select mergeG.*,
--case when tempTelGroup.mergegroup is null then mergeG.namegroup
-- else tempTelGroup.mergegroup
--end as CheckGroup
--from mergeG
--left join
--(
----Get telCountnum
--select telg.telgroup,MIN(mergeG.namegroup) as mergegroup
--from mergeG
--join telg on mergeG.telgroup=telg.telgroup and (mergeG.id!=telg.id)
--group by telg.telgroup) as tempTelGroup on mergeG.telgroup=tempTelGroup.telgroup
--) as t
--group by CheckGroup
select mergeG.*,
case when tempTelGroup.mergegroup is null then mergeG.namegroup
else tempTelGroup.mergegroup
end as CheckGroup
from mergeG
left join
(
--Get telCountnum
select telg.telgroup,MIN(mergeG.namegroup) as mergegroup
from mergeG
join telg on mergeG.telgroup=telg.telgroup and (mergeG.id!=telg.id)
group by telg.telgroup) as tempTelGroup on mergeG.telgroup=tempTelGroup.telgroup
order by CheckGroup,id desc
--大版V5
;with a as
(
select a.*,b.id bid from t a
inner join t b
on (a.name = b.name or a.tel = b.tel) and a.id<>b.id
)
select distinct id from a a2
where not exists(select 1 from a
where (a.name = a2.name or a.tel = a2.tel) and a.bid>a2.id)
/*
id
-----------
3
7
*/
declare @name varchar(8)
set @name='test'
;with maco as
(
select id,name,tel from t where name=@name
union all
select a.* from t a ,maco b where a.tel=b.tel
and a.id not in (select b.id from b)
union all
select a.* from t a ,maco b where a.name=b.name
and a.id not in (select b.id from b)
)
select * from maco
--3/6/7
select * from t as a
where not exists(select 1 from t as b where b.name=a.name and b.tel>a.tel)
and not exists(select 1 from t as c where c.tel=a.tel and c.name>a.name)
--3/7
select * from t as a
where exists(select 1 from t as b where (b.name=a.name and b.tel<a.tel) or( b.tel=a.tel and b.name<a.name))
and not exists(select 1 from t as c where c.tel=a.tel and c.name>a.name)
-- =============================================
-- Author: <maco_wang>
-- Create date: <2012-08-13>
-- Description: <抛砖引玉,代码凌乱,望见谅>
-- 这个递归貌似用cte表达式理论上绝对可以的,但是我没写成功
-- =============================================
create proc zhangandli_t
as
begin
--创建个临时表
create table #t
(id int ,name varchar(8),tel varchar(6),groupno int default 0)
--把数据插入临时表
insert into #t(id,name,tel) select * from t
--定义组号
declare @groupno int set @groupno=1
--定义两个变量
declare @name varchar(8),@tel varchar(8)
--定义每组的起始序号
declare @i int
maco:
select @i=min(id) from #t where groupno=0
update #t set groupno=@groupno where id=@i
select @name=name from #t where groupno=@groupno
update #t set groupno=@groupno where name=@name
first:
while((select count(1) from #t where groupno=0
and tel in (select tel from #t where groupno=@groupno))>0)
begin
update #t set groupno=@groupno
where groupno=0
and tel in (select tel from #t where groupno=@groupno)
goto second
end
second:
while((select count(1) from #t where groupno=0
and name in (select name from #t where groupno=@groupno))>0)
begin
update #t set groupno=@groupno
where groupno=0
and name in (select name from #t where groupno=@groupno)
goto first
end
--查看是否有没有编组的
if(exists(select 1 from #t where groupno=0))
begin
set @groupno=@groupno+1
goto maco
end
select * from #t
/*
id name tel groupno
----------- -------- ------ -----------
1 zhang 11111 1
2 zhang 11111 1
3 zhang 22222 1
4 test 33333 2
5 test 12345 2
6 test1 55555 3
7 test3 33333 2
*/
--查看重复数据的最大值
select max(id) as id from #t group by groupno having(count(1)>1)
/*
id
-----------
3
7
*/
--查看重复数据的最大值
select max(id) as id from #t group by groupno
/*
id
-----------
3
7
6
*/
--删除临时表
drop table #t
end
go
exec zhangandli_t