2
DECLARE @ProductFeature TABLE (ProductID int, FeatureID int)

INSERT INTO @ProductFeature
  SELECT 1,100
    UNION ALL
  SELECT 1,101
    UNION ALL
  SELECT 1,102
    UNION ALL
  SELECT 2,103
    UNION ALL
  SELECT 2,104
    UNION ALL
  SELECT 3,100
    UNION ALL
  SELECT 3,101
    UNION ALL
  SELECT 3,102
    UNION ALL
  SELECT 4,102
    UNION ALL
  SELECT 4,101
    UNION ALL
  SELECT 5,110
    UNION ALL
  SELECT 5,100
    UNION ALL
  SELECT 5,101

私の要件は、合格した場合、と同様の機能ProductID = 1を選択する必要があることです。ProductProductID = 1

ProductID = 13つの機能(100,101,102)があるので、同じProductID = 3数と機能を持つものだけがありますProductID = 1

期待される結果

ProductID   FeatureID
3              100
3              101
3              102
4

4 に答える 4

2

EXCEPT操作を伴うオプション

DECLARE @ProductID int = 1

SELECT ProductID, FeatureID
FROM ProductFeature p1
WHERE p1.ProductID != @ProductID AND 
      NOT EXISTS (
                  SELECT p2.FeatureID         
                  FROM ProductFeature p2
                  WHERE p2.ProductID = @ProductID                                                
                  EXCEPT
                  SELECT p3.FeatureID
                  FROM ProductFeature p3
                  WHERE p3.ProductID = p1.ProductID               
                  )
于 2013-01-28T12:33:00.183 に答える
1

通常、私はcteビットクリアラーを使用します(遅い/速いかどうかはわかりません)。

    DECLARE @fromProductID int = 1;

    with cteGroup (ProductID) AS
     (
        select ProductID
        from @ProductFeature
        where FeatureID in (select FeatureID
                        from @ProductFeature 
                        where ProductID = @fromProductID)
           and ProductID <> @fromProductID
        group by ProductID
        having COUNT(FeatureID)= (select COUNT(FeatureID) as NoOfRecords
                                    from @ProductFeature 
                                  where ProductID = @fromProductID) 
    ) 
     select a.ProductID,b.FeatureID
     from cteGroup a
     inner join @ProductFeature b
     on a.ProductID = b.ProductID
于 2013-01-28T07:52:24.523 に答える
1

最初に、少なくとも1つの機能を共有する製品を決定する必要があります。次に、これらの製品から、まったく同じ数の機能を持つ製品を見つけます。

これでうまくいくはずです:

DECLARE @productID int = 1

SELECT
  [p3].[ProductID],
  [p3].[FeatureID]
FROM
(
  SELECT 
    [p1].[ProductID]
  FROM [ProductFeature] [p1]
  INNER JOIN [ProductFeature] [p2] ON [p1].[FeatureID] = [p2].[FeatureID]
  WHERE [p1].[ProductID] <> [p2].[ProductID] AND [p2].[ProductID] = @productID
) AS [sub]
INNER JOIN [ProductFeature] [p3] ON [sub].[ProductID] = [p3].[ProductID]
GROUP BY
  [p3].[ProductID],
  [p3].[FeatureID]
HAVING COUNT(*) = (SELECT COUNT(*)
                   FROM [ProductFeature]
                   WHERE [ProductID] = @productID)
ORDER BY 
  [p3].[ProductID] ASC,
  [p3].[FeatureID] ASC

ここフィドルを探してください。

于 2013-01-28T07:19:46.787 に答える
1

それは少し効率的ですが、うまくいきます

select pr.ProductID , pr.FeatureID
from @ProductFeature pr
where pr.ProductID in (
    select prd.ProductID
    from @ProductFeature pr
    join @ProductFeature prd
    on pr.ProductID != prd.ProductID
    and pr.FeatureID = prd.FeatureID
    where pr.ProductID = @ProductId
    group by prd.ProductID
    having count(prd.ProductID) = (select count(distinct pr.FeatureID) from @ProductFeature pr where pr.ProductID = @ProductId)
    )
于 2013-01-28T07:17:18.443 に答える