SQL Server查询 - 如何修剪表

时间:2015-03-27 08:22:54

标签: sql sql-server-2005 apriori

我有一张表'L2'

Itemset      Itemset2      
   1             3
   2             3
   2             5
   3             5

我在表'c3'

的列中为这些值创建了[三对一组合]
Itemset       Itemset2       itemset3
   1              3             5
   2              3             5
   1              2             3
   1              2             5

就像Apriori一样,我想修剪表C3。

即。将此表作为输出'C3Prune'

 Itemset       Itemset2       itemset3
   2              3             5

我想为同一个创建一个SQL Server查询,我试过循环,但它不正确。

2 个答案:

答案 0 :(得分:1)

this answer中提供了两种方式,我想使用UNIONEXCEPT提供另一个查询:

  select *
  from C3
  where not exists
  (
    select c3.itemset a, c3.itemset2 b 
    union
    select c3.itemset a, c3.itemset3 b 
    union
    select c3.itemset2 a, c3.itemset3 b 
    except
    select itemset a, itemset2 b from l2
   )

输出:

2   3   5

SQLFIDDLE DEMO

答案 1 :(得分:0)

试试这个:

DECLARE @L2 TABLE ( I1 INT, I2 INT )
DECLARE @C3 TABLE ( I1 INT, I2 INT, I3 INT )

INSERT  INTO @L2
VALUES  ( 1, 3 ),
        ( 2, 3 ),
        ( 2, 5 ),
        ( 3, 5 )

INSERT  INTO @C3
VALUES  ( 1, 3, 5 ),
        ( 2, 3, 5 ),
        ( 1, 2, 3 ),
        ( 1, 2, 5 )


--Version 1
SELECT  c.*
FROM    @C3 c
        JOIN @L2 l1 ON c.I1 = l1.I1 AND c.I2 = l1.I2
        JOIN @L2 l2 ON c.I1 = l2.I1 AND c.I3 = l2.I2
        JOIN @L2 l3 ON c.I2 = l3.I1 AND c.I3 = l3.I2

--Version 2
SELECT  * FROM @C3 c
WHERE 
        EXISTS(SELECT * FROM @L2 WHERE I1 = c.I1 AND I2 = c.I2) AND
        EXISTS(SELECT * FROM @L2 WHERE I1 = c.I1 AND I2 = c.I3) AND
        EXISTS(SELECT * FROM @L2 WHERE I1 = c.I2 AND I2 = c.I3)

输出:

I1  I2  I3
2   3   5