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) 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -