如何检查多列如果为空然后按顺序使用下一列?

How to check multi columns if null then use the next column in order?

我有 4 列,如果第 1 列不为空,则使用它,否则使用第 2 列,然后如果第 2 列为空,则使用第 3 列,如果第 3 列为空,则使用第 4 列,否则留空。

这是我所拥有的,但没有检查所有列。

Case WHEN SEGMENT1 IS Null THEN SEGMENT2 WHEN SEGMENT2 IS Null THEN SEGMENT3 WHEN SEGMENT3 IS Null THEN SEGMENT4 Else SEGMENT1 END AS Segment

谢谢!

您描述的逻辑如下所示:

(CASE WHEN SEGMENT1 IS NOT Null THEN SEGMENT1
      WHEN SEGMENT2 IS NOT Null THEN SEGMENT2
      WHEN SEGMENT3 IS NOT Null THEN SEGMENT3
      ELSE SEGMENT4
 END) AS Segment

考虑 COALESCE(ISO/ANSI 标准 SQL 方法),如文档中所述:

The arguments are evaluated in the order in which they are specified, and the result of the function is the first argument that is not null. The result can be null only if all arguments can be null.

COALESCE(SEGMENT1, SEGMENT2, SEGMENT3, SEGMENT4) AS Segment