Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok where ADVERTFACETTERMASSIGNMENT is {AdvertId, FacetTermId} for a two term search...</p> <pre><code>select fta1.AdvertID from ADVERTFACETTERMASSIGNMENT fta1 join ADVERTFACETTERMASSIGNMENT fta2 on fta1.AdvertID = fta2.AdvertID where fta1.FacetTermId = @searchFacet1 and fta2.FacetTermID = @searchFacet2 and fta1.AdvertID &lt;&gt; @searchAdvertId </code></pre> <p>a general answer with working example:</p> <pre><code>declare @AdvertFacetTermAssignment table (AdvertId int, FacetTermId int) insert into @AdvertFacetTermAssignment values (1,10), (1,11), (2,10), (3,11), (4,10), (4,11), (5,10), (5,11), (5,12), (6,10), (6,12), (6,13), (7, 10), (7, 11), (8, 12), (9,10), (9,11), (9,12), (10, 10), (10,12) declare @searchAdvertId int = 1 declare @targetMatch int = (select COUNT(*) from @AdvertFacetTermAssignment where AdvertId = @searchAdvertId) select aft2.AdvertId from @AdvertFacetTermAssignment aft1 join @AdvertFacetTermAssignment aft2 on aft1.FacetTermId = aft2.FacetTermId and aft1.AdvertId &lt;&gt; aft2.AdvertId where aft1.AdvertId = @searchAdvertId group by aft2.AdvertId having COUNT(*) = @targetMatch </code></pre> <p>results = 4,5,7,9</p> <p>This last one is not what was requested, but grabs everything similar (some matching facets) and orders by how similar.<br> (all matches are treated equally)</p> <pre><code> select aft2.AdvertId, COUNT(aft1.AdvertId) as matches, ABS(COUNT(*)-@targetMatch) as nonMatches from @AdvertFacetTermAssignment aft1 right outer join @AdvertFacetTermAssignment aft2 on aft2.FacetTermId = aft1.FacetTermId and aft1.AdvertId = @searchAdvertId and aft2.AdvertId &lt;&gt; @searchAdvertId group by aft2.AdvertId having COUNT(aft1.AdvertId) &gt; 0 order by COUNT(aft1.AdvertId) DESC, ABS(COUNT(*)-@targetMatch) ASC </code></pre> <p>results: </p> <pre><code> AdvertId matches nonMatches 4 2 0 7 2 0 9 2 1 5 2 1 10 1 0 6 1 1 2 1 1 3 1 1 </code></pre> <p>(btw I'm posting this from Sussex, WI)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload