Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 案例和逻辑相似但产生不同结果的案例_Sql_Case_Where_Teradata - Fatal编程技术网

Sql 案例和逻辑相似但产生不同结果的案例

Sql 案例和逻辑相似但产生不同结果的案例,sql,case,where,teradata,Sql,Case,Where,Teradata,我收到一个同事的查询,其中有一个where语句,如下所示: where ... and case when AOS.aircft_out_of_srvc_reason_cd in ('L','H') or AOS.mntnc_stn_cd in ('TLE','DWH') then 'Y' else 'N' end ='N' 我想如果我把它改成下面的话,我可以把箱子拿出来 where ... and not ( AOS.aircft_out_of_s

我收到一个同事的查询,其中有一个
where
语句,如下所示:

where ...
and case
    when    AOS.aircft_out_of_srvc_reason_cd in ('L','H')
         or AOS.mntnc_stn_cd in ('TLE','DWH') then 'Y'
    else 'N' end ='N'
我想如果我把它改成下面的话,我可以把
箱子拿出来

where ...
and not (   AOS.aircft_out_of_srvc_reason_cd in ('L','H')
         or AOS.mntnc_stn_cd in ('TLE','DWH'))

但不知何故,这只产生了不到原始记录数量10%的收益。在我看来,两者的逻辑似乎是相同的。有人知道为什么Teradata会以不同的方式对待他们吗?

这方面的等效逻辑是:

where ... and
     (case when AOS.aircft_out_of_srvc_reason_cd in ('L', 'H') or
                AOS.mntnc_stn_cd in ('TLE', 'DWH')
           then 'Y'
           else 'N'
      end) = 'N'
基本上是:

where . . . and
      AOS.aircft_out_of_srvc_reason_cd not in ('L', 'H') and
      AOS.mntnc_stn_cd not in ('TLE', 'DWH')
(与您的
not
表达式相同)

唯一的问题是,如果两列都是
NULL
,那么您应该包括:

where . . . and
      ( (AOS.aircft_out_of_srvc_reason_cd not in ('L', 'H') and
         AOS.mntnc_stn_cd not in ('TLE', 'DWH') and
        ) or
        (AOS.aircft_out_of_srvc_reason_cd is null and AOS.mntnc_stn_cd is null)
      )

您能指出第一个查询选择的一个示例行,而不是第二个吗?是否有任何列
aircft\u out\u of_srvc\u reason\u cd
mntnc\u stn\u cd
允许空值?
是好的。它被
否定,而不是(…)
是的,我认为问题的根本原因是空值,即使Eric没有响应。where的这部分中的列不在原始查询中。在包含它们之后,很明显这两列要么都为null,要么都不为null。不过我还是不明白,(x,y,z)中的
null还是(a,b,c)中的null
返回true?@EricEdLohmar。否。几乎任何包含
NULL
的比较都返回
NULL
,在
WHERE
子句中,该值被视为false。