如何在列具有字符串值的情况下使用解码函数

How to use Decode Function in the case where column has string values

我有一个视图 xxabc_v(如下所示),当“值”列总和 (900+(-900 )=0) 对于“日期”01-Apr-21 的“field_name”值(需求 A+需求 B)变为零。

在上述情况下,如何将解码逻辑放入代码列?

Table 结构和预期输出:

你不需要 decode() 因为有一个更简单的方法:

select nullif(code, 'N/A')

此returnsNULLcode取指定值时

如果你真的想改变数据,那么你想要update:

update t
    set code = NULL
    where code = 'N/A';

编辑:

我明白了,你有一个额外的条件。所以,使用 case:

(case when code = 'N/A' and
           sum(value) over (partition by id, date) = 0
      then NULL
      else code
 end)

我假设您需要在 sum() 时进行日期明智的 id 明智的总结。请检查一下:

select date,id,(case when sum(value)over(partition by date,id)=0 and code='N/A' then NULL
else Code end)code, field_name,value
from tablename