sql - Retrieve all records sharing duplicate value from a column -
i return records table share duplicate values given specific column, in case bin column.
create table #tempcompany (companyid varchar(6), name varchar(50),bin varchar(6)) insert #tempcompany (companyid,name,bin) select '000001','abc company','000000' union select '000002','def company','000001' union select '000003','ghi company','000001' union select '000004','jkl company','000002' union select '000005','lmn company','000003' union select '000006','opq company','000003' union select '000007','rst company','000003' drop table #tempcompany i return result:
companyid name bin 000002 def company 000001 000003 ghi company 000001 000005 lmn company 000003 000006 opq company 000003 000007 rst company 000003
you can using window functions:
select tc.* (select tc.*, count(*) on (partition bin) cnt #tempcompany tc ) tc cnt >= 2;
Comments
Post a Comment