首页 > 解决方案 > 如果列已填充,则创建总accordif

问题描述

我想计算参加活动的总人数,这个数字是由邀请的人和将被邀请的人数计算的。所以我有这个信息,

 refInqCredLink RegisterDate    Name    Phone   Email   Horario Insc1   Insc2   Insc3
496 2019-08-29 15:38:13.183 Abilio  91 abilio@hotmail.com   3   albano  jorge   
497 2019-08-30 14:12:46.873 Duarte  25 duarte@sapo.pt   3   antonio     
499 2019-08-30 14:48:29.067 AGOSTINHO 92 agostinho@gmail.com    1   Jorge Antonio   Manuel Fernando     

在这种情况下,“John”将出席活动并邀请“albano”和“jorge”。所以这条线的总人数是3。

我需要检查 Insc1、Insc2 和 Insc3 列是否有值,如果有,则 (<>'') 算作 1 人。我需要使用此总和在此表中创建一个视图。

如果我尝试这段代码,

Select 
SUM(case when not Name is null  then 1 else 0 end
+ 
case when not Insc1 is null then 1 else 0 end
+ 
case when not Insc2 is null then 1 else 0 end
+ 
case when not Insc3 is null then 1 else 0 end) 
from LACTINFO_InquiryCredential_Link_Seminario

这将返回 12 而不是 8。

我有 3 个名字 + 5 个受邀者 = 8

标签: sql

解决方案


尝试以下

Select 
SUM(case when Insc1 is null or Insc1 = ''  then 0 else 1 end
+ 
case when Insc2 is null  or Insc2 = ''  then 0 else 1 end
+ 
case when Insc3 is null  or Insc3 = ''  then 0 else 1 end) 
+
count(*)
from tab

推荐阅读