ABinitio如何从多列列表中返回首个非空列值?
ABinitio 实现多列取首个非空值的方法
ABinitio没有直接提供类似Teradata Coalesce或Oracle NVL的多参数取首个非空值函数,但可以通过以下两种方式实现你的需求:
嵌套条件判断(快速实现)
针对6列的场景,直接用嵌套if语句就能快速完成,逻辑清晰直观:if (col1 is not null) col1 else if (col2 is not null) col2 else if (col3 is not null) col3 else if (col4 is not null) col4 else if (col5 is not null) col5 else col6字段类型不局限于字符串,数值、日期等类型都适用,只要保持前后字段类型一致即可。
自定义可复用函数(适合多场景复用)
如果需要在多个地方复用这个逻辑,可以创建一个自定义函数,支持任意数量的同类型参数:define string function first_non_null(string args...) begin integer i; for i from 1 to length(args) do if args[i] is not null then return args[i]; end for; return null; end;使用时直接传入你的字段即可:
first_non_null(col1, col2, col3, col4, col5, col6)
注意:如果处理的是非字符串类型(比如数值、日期),需要把函数定义里的string替换成对应的数据类型。
内容的提问来源于stack exchange,提问作者Shirin BS
相关产品推荐
相关产品推荐

