问题是我有 3 个条件好坏丑。我的目标是,如果以下条件之一为 null,则将 null 替换为 0,否则如果所有条件均为 null,则将其显示为 null
case when 'Good' is not null or 'Bad' is not null or 'Ugly' is not null
then coalesce(value,0)
else value
end result
这里的问题是即使条件将所有空值都变为 0,这不是我想要的。提前致谢
我想也许你想要这样的东西。如果我得到更多细节,我会更新。
如果我只是离开这个评论
问题是,如果同一产品的好是空,坏和丑陋的值是 0-100,那么我希望将空值替换为 0。如果对于同一个产品,这三个都是空值,那么我希望它们为空值并且不为 0
我会给你这样的东西
case
when good is not null then good
when good is null and coalesce(bad,-1) between (0 and 100) and coalesce( ugly,-1) between (0 and 100) then 0
else null
end
如果您尝试提供所有 3 列(我假设您有多个列),您可能会执行类似的操作。
select
case
when good is null and bad is null and ugly is null then null
when good is null and (bad is not null or ugly is not null) then 0
else good
end as 'good column values'
,
case
when good is null and bad is null and ugly is null then null
when bad is null and (good is not null or ugly is not null) then 0
else bad
end as 'bad column values'
,
case
when good is null and bad is null and ugly is null then null
when ugly is null and (good is not null or bad is not null) then 0
else ugly
end as 'ugly column values'
您当前的表达式说(在伪代码中):
IF <something is true>
IF value IS NULL
RETURN 0
ELSE
RETURN value
ENDIF
ELSE
RETURN value
ENDIF
那是你想要的吗?
Your code says "if one of good, bad, or ugly is not null" but your question says "if one of good, bad, or ugly is null"
If I'm understanding you correctly, just take out the nots.
Edit: I'm also assuming that you don't actually mean to use the literal strings "Good", "Bad" and "Ugly", but that they are meant to represent column names or something.
You should be aware that using these strings means that your code will never enter the "else" condition, as "Good", "Bad" and "Ugly" are not null.
EDIT 2: take your column names (good, bad, ugly) out of quotes so they will actually be evaluated. This is why you're still seeing that error.