首页 > 解决方案 > 查找一行中空列的总和

问题描述

我想创建一个列来查找 postgres 表中非空列的计数(如下所示)。你能给我一个提示或解决方案吗?

在此处输入图像描述

标签: sqlpostgresql

解决方案


您可以始终使用 GENERATED 作为列。例子:

create table test (a VARCHAR (50),
b VARCHAR (50),
c VARCHAR (50),
cnt numeric GENERATED always as  (

  case when  a is null then 0 else 1 end +
  case when  b is null then 0 else 1 end +
  case when  c is null then 0 else 1 end
) STORED);

链接:https ://www.db-fiddle.com/f/fU5LLuh5gvwkKhhRbeCyh8/0


推荐阅读