I have the following tables:
Order
----
ID (pk)
OrderItem
----
OrderID (fk -> Order.ID)
ItemID (fk -> Item.ID)
Quantity
Item
----
ID (pk)
Hmm, funny, I am currently working on something similar. Why don't you just join the sample order (i.e. their items) with all other orders (their items) and list all orders which have at least 85% matches by grouping the count of matches per order?
-- let @SampleorderID be the ID of a sample
declare @totalOrders int, @ThresholdOrderCount int
select @totalOrders = count(*) from OrderItems where orderID=@SampleOrderID
set @ThresholdOrderCount = 85*@totalOrders/100 -- 85% of the item amount of the sample
-- Now match the contents of the sample order with the contents of all other orders
-- count the #matches and show only those orders with at least 85% identical items
Select AllOrder.OrderID, count(*)
from OrderItems sample
join OrderItems AllOrder
on sample.ItemID = AllOrder.ItemID
where sample.OrderID = @SampleOrderID
and sample.OrderID<>AllOrder.OrderID
group by AllOrder.OrderID
having count(*)>@ThresholdOrderCount
This should work. However, it also returns orders, which contain more items than the sample. If that is ok, then the above query should also be quite fast.