Teradata SQL 而不是 vs and

Teradata SQL and not vs and

我有一个 table 我正在尝试拆分。

原来的 table 是 1,390k 行,我有三个条件 A、B 和 C,我想用它们来拆分 table。

这个查询 returns 60k 行

sel stuff
from table
where  ( A and C)
and (B and C) 

此查询 returns 1,060k 行:

sel stuff
from table
where  not ( A and C)
and not (B and C) 

我的问题是为什么第二个查询返回 1,060k 行,而不是我预期的 1,330k?

x and y的反义词是not x OR not y

(A & C) & (B & C)的反义词不是!(A & C) & !(B & C)而是!((A & C) & (B & C))

由于没有共享示例数据,我假设您的 table 具有以下示例数据。

a   b   c
---------
0   0   0
1   0   0
0   1   0
0   0   1
1   1   0
0   1   1
1   0   1
1   1   1

您的原查询如下

select * from t1
where ( A=1 and C=1)
and (B=1 and C=1);

结果 1:

a   b   c
---------
1   1   1

您建议生成与上述查询相反的结果的查询如下,它永远不会生成您想要的结果。

select * from t1
where not(A=1 and C=1)
and not(B=1 and C=1);

结果二:

a   b   c
---------
0   0   0
1   0   0
0   1   0
0   0   1
1   1   0

以下任何查询都会生成与您的原始查询相反的结果。

select * from t1
where not(( A=1 and C=1)
and (B=1 and C=1));

select * from t1
where A<>1 or B<>1 or C<>1;

结果 3:

a   b   c
---------
0   0   0
1   0   0
0   1   0
0   0   1
1   1   0
0   1   1
1   0   1

从上面的查询结果可以看出,result 1result 3正好相反。您可以查看演示 here

我看到这已经有了答案。但是,我认为值得一提的是一种方法,它不需要您过多思考原始查询的逻辑逆是什么。

如果第二个查询的目标只是获取第一个查询中没有的所有内容,也许您的第二个查询应该是这样的:

sel stuff --This part gets the new records
from table
Minus --This part is the original query and excludes those records
sel stuff
from table
where  ( A and C)
and (B and C)