SQL 服务器查询- 如何修剪 table
SQL Server Query- How to Prune a table
我有一个Table'L2'
Itemset Itemset2
1 3
2 3
2 5
3 5
我在 table 'c3'
的列中为这些值创建了一个 [三对组合]
Itemset Itemset2 itemset3
1 3 5
2 3 5
1 2 3
1 2 5
就像在 Apriori 中一样,我想修剪 table C3。
即将此 table 作为输出 'C3Prune'
Itemset Itemset2 itemset3
2 3 5
我想为此创建一个 SQL 服务器查询,我尝试了循环,但它不正确。
试试这个:
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
中提供了2种方法,我想用UNION
和EXCEPT
来给出另一个查询:
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
我有一个Table'L2'
Itemset Itemset2
1 3
2 3
2 5
3 5
我在 table 'c3'
的列中为这些值创建了一个 [三对组合]Itemset Itemset2 itemset3
1 3 5
2 3 5
1 2 3
1 2 5
就像在 Apriori 中一样,我想修剪 table C3。
即将此 table 作为输出 'C3Prune'
Itemset Itemset2 itemset3
2 3 5
我想为此创建一个 SQL 服务器查询,我尝试了循环,但它不正确。
试试这个:
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
UNION
和EXCEPT
来给出另一个查询:
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