比较多个可以为NULL的列

时间:2018-06-28 12:55:11

标签: sql logic teradata

我想比较一组列并决定:

  1. 如果所有非NULL的列都相等
  2. 如果任何非NULL的列都不相等
  3. 如果所有列均为NULL

示例输入和输出:

<input date-range-picker="" readonly="" class="form-control input-date-fields-readonly ng-pristine ng-valid ng-isolate-scope ng-empty ng-touched" type="text" ng-model="model[options.key]" options="daterangepicker.options" id="formly_1_daterangepicker_caseCreationRange_4" name="formly_1_daterangepicker_caseCreationRange_4" formly-custom-validation="" tabindex="9" aria-describedby="formly_1_daterangepicker_caseCreationRange_4_description" style="">

我需要测试的列数很多,因此比较每一个的逻辑语句有点麻烦。

系统:Teradata SQL数据库

1 个答案:

答案 0 :(得分:1)

您将使用case表达式。也许:

select (case when a is null and b is null and c is null and d is null
             then 'all null'
             when coalesce(a, b, c, d) = coalesce(b, c, d, a) and
                  coalesce(a, b, c, d) = coalesce(c, d, b, a) and
                  coalesce(a, b, c, d) = coalesce(d, a, b, c)
             then 'all non-nulls are equal'
             else 'unequal'
         end);