WHERE子句中的CASE语句:Teradata

时间:2016-11-18 06:47:31

标签: sql teradata data-warehouse

下面粘贴的是一个示例SQL代码,该代码在where子句中使用case语句,但是它抛出了一个语法错误,说“在mrktng_pckge_typ_cd和NOT关键字之间期待像END这样的关键字。”

CASE WHEN exc_ind=1 THEN mrktng_pckge_typ_cd NOT IN ('a','b','c','d') ELSE NULL END

2 个答案:

答案 0 :(得分:5)

where      exc_ind <> 1 
        or mrktng_pckge_typ_cd NOT IN ('a','b','c','d') 

如果exc_ind可以为NULL -

where      coalesce (exc_ind,-1) <> 1 
        or mrktng_pckge_typ_cd NOT IN ('a','b','c','d') 

出于演示目的:

where case when coalesce (exc_ind,-1) <> 1 or mrktng_pckge_typ_cd NOT IN ('a','b','c','d') then 1 else 0 end = 1

答案 1 :(得分:-1)

您可能需要这样做。我给出了不同的表名,但你可以这样做:

Declare @condition as int = 1
SELECT *
FROM mstCity
WHERE( (1=@condition and Name IN (SELECT AliasCity.Name From mstCity AliasCity WHERE AliasCity.Name NOT IN ('USA','UK') )) OR 
       (2=@condition AND Name IN (SELECT AliasCity.Name From mstCity AliasCity  ))
     )

根据您的查询更新的答案应如下所示:

WHERE ( ( exc_ind= 1 AND mrktng_pckge_typ_cd NOT IN ('a','b','c','d')) OR 
    ( exc_ind <> 1 AND 1=1 )
  )