How to select similar sets in SQL

后端 未结 8 1732
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 15:38

I have the following tables:

Order
----
ID (pk)

OrderItem
----
OrderID (fk -> Order.ID)
ItemID (fk -> Item.ID)
Quantity

Item
----
ID (pk)
         


        
8条回答
  •  遥遥无期
    2020-12-13 15:50

    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.

提交回复
热议问题