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。