首页 > 解决方案 > 计算 teradata 中每行的条目数

问题描述

我有一个表格,每个客户都有一行,列是关于该客户的属性。我想知道每个客户填充了多少属性......即不是空值。

作为一个例子,我有这个:

在此处输入图像描述

我希望我的输出是这样的:

在此处输入图像描述

这个的sql是什么?

标签: sqlteradata

解决方案


您可以使用case表达式:

select customer,
       (case when col1 is not null then 1 else 0 end +
        case when col2 is not null then 1 else 0 end +
        case when col3 is not null then 1 else 0 end +
        case when col4 is not null then 1 else 0 end +
        case when col5 is not null then 1 else 0 end
       ) as num_entries
from t;
    

推荐阅读