从sql server 2008中的多个值中选择值

时间:2013-01-28 06:58:03

标签: sql sql-server-2008

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,那么我必须选择Product,其功能与ProductID = 1类似。

由于ProductID = 1有3个功能(100,101,102),因此只有ProductID = 3具有相同的计数和功能ProductID = 1

预期结果

ProductID   FeatureID
3              100
3              101
3              102

4 个答案:

答案 0 :(得分: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               
                  )

答案 1 :(得分: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)
    )

答案 2 :(得分: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

查看here Fiddle

答案 3 :(得分:1)

通常我使用cte -bit更清晰(不知道是否更慢/更快)。

    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