ID ID_1 ID_2
RRR010464 0000009 2000001
RRR010466 0000009 2000001
RRR073918 0000008 1000000
RRR073918 0000008 1000000
RRR074870 0000006 3000000
RRR074870 0000005 3000000
RRR017646 0000004 1000000结果要
RRR010464 0000009 2000001
RRR010466 0000009 2000001
RRR073918 0000008 1000000
RRR073918 0000008 1000000意思就是要 ID_2相同 ID_1D的后面4位相同的结果
RRR010464 0000009 2000001
RRR010466 0000009 2000001
RRR073918 0000008 1000000
RRR073918 0000008 1000000
RRR074870 0000006 3000000
RRR074870 0000005 3000000
RRR017646 0000004 1000000结果要
RRR010464 0000009 2000001
RRR010466 0000009 2000001
RRR073918 0000008 1000000
RRR073918 0000008 1000000意思就是要 ID_2相同 ID_1D的后面4位相同的结果
FROM TABLE_A A
INNER JOIN TABLE_A B
ON A.ID_1 = B.ID_1 AND A.ID_2 = B.ID_2
WHERE A.ID <> B.ID
where a.id_2=b.id_2 and right(a.id,4)=right(b.id,4)
from tabe a
inner join table b on a.ID_2= b.ID_2 and right(a.ID_1,4) = right(b.ID_1,4)
drop table tb
go
create table tb(id varchar(50),ID_1 varchar(50),ID_2 varchar(50))
insert into tb select 'RRR010464','0000009','2000001'
insert into tb select 'RRR010464','0000009','2000001'
insert into tb select 'RRR073918','0000008','1000000'
insert into tb select 'RRR073918','0000008','1000000'
insert into tb select 'RRR074870','0000006','3000000'
insert into tb select 'RRR074870','0000005','3000000'
insert into tb select 'RRR017646','0000004','1000000'select * from tb t where exists(
select 1 from tb where right(id_1,4)=right(t.id_1,4)
and id_2=t.id_2 group by id_2,right(id_1,4)
having count(1)>1)
大鸟->大大鸟->大大大鸟
大鸟->大大鸟->大大大鸟
大鸟->大大鸟->大大大鸟