我有一个case语句,它在列中返回空值,而我在执行case语句时不需要空值。
SELECT ABC
(
CASE
WHEN
condition
THEN 1
WHEN
condition2
THEN 2
END
) AS column_name FROM tablename;
Column_name也返回空值
所以我将查询更改为
SELECT ABC
(
CASE
WHEN
condition
THEN 1
WHEN
condition2
THEN 2
else 3
END
) AS column_name FROM tablename where column_name <>3;
我收到错误'无效标识符'。
对此有何想法?
答案 0 :(得分:2)
您在ABC
之后缺少逗号(假设ABC
是一列):
SELECT ABC , --<---- comma added here
(
CASE
WHEN
condition
THEN 1
WHEN
condition2
THEN 2
ELSE 3
END
) AS column_name
FROM tablename;
您也可以删除那些不需要的括号。
不清楚为什么添加WHERE column_name <> 3
。经过调整后的CASE
将NULL
转为3
。你想要显示那些行吗?如果是,请保持上述查询。如果没有,你可以使用它:
SELECT
ABC ,
CASE
WHEN
condition
THEN 1
WHEN
condition2
THEN 2
ELSE 3
END AS column_name
FROM tablename
WHERE (condition) OR (condition2) ;
或(更接近你的逻辑):
SELECT *
FROM
( SELECT
ABC ,
CASE
WHEN
condition
THEN 1
WHEN
condition2
THEN 2
ELSE 3
END AS column_name
FROM tablename
) AS tmp
WHERE column_name <> 3 ;
答案 1 :(得分:1)
您可以使用NVL(something, 0)
,如果something
生成NULL
,您将获得0。