Case 和 Where 逻辑看起来相似但产生不同的结果
Case and Where logic appear similar but yield different results
我收到同事的查询,其中包含如下 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'
我想我可以把case
改成下面的
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)
)
我收到同事的查询,其中包含如下 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'
我想我可以把case
改成下面的
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)
)