sql - Find matching sets in a database table -
i have junction table in (sql server 2014) database columns firstid , secondid. given specific firstid, i'd find other firstids table have equivalent set of secondids (even if set empty). sample data:
firstid secondid 1 1 1 2 2 3 3 1 3 2 ... ...
in case of sample data, if specified firstid = 1, i'd expect 3 appear in result set.
i've tried following far, works pretty except empty sets:
select firstsecondequalset.firstid from firstsecond firstsecondoriginal inner join firstsecond firstsecondequalset on firstsecondoriginal.secondid = firstsecondequalset.secondid where firstsecondoriginal.firstid = @firstid and firstsecondequalset.firstid != @firstid group by firstsecondequalset.firstid having count(1) = (select count(1) firstsecond firstsecond.firstid = @firstid)
i think it's somehow related relational division no remainder (rdnr). see great article dwain camps reference.
declare @firstid int = 1 select f2.firstid firstsecond f1 inner join firstsecond f2 on f2.secondid = f1.secondid , f1.firstid <> f2.firstid f1.firstid = @firstid group f2.firstid having count(*) = (select count(*) firstsecond firstid = @firstid)