问个 简单的 sql 条件表t1有8列(a,b,c,d,e,f,g,h)。前7列如果有两列不为空 则对 h 列求和,这样的sql语句应该怎样写比较简便?难道要select sum(h) from t1where (a>0 and b>0) or (a>0 and c>0)....这样枚举下去吗? [解决办法]
Quote: 引用:完善一下:select sum(case when (a>0 and b>0) or (a>0 and c>0) or (a>0 and d>0) or (a>0 and e>0) or (a>0 and f>0) or (a>0 and g>0) or ... then h else 0 end) as hfrom T1将条件放在后面也是一样的。也就是说一定要将条件一个个写出来吗?没有别的办法啦?
完善一下:select sum(case when (a>0 and b>0) or (a>0 and c>0) or (a>0 and d>0) or (a>0 and e>0) or (a>0 and f>0) or (a>0 and g>0) or ... then h else 0 end) as hfrom T1
select sum(case when (a>0 and b>0) or (a>0 and c>0) or (a>0 and d>0) or (a>0 and e>0) or (a>0 and f>0) or (a>0 and g>0) or ... then h else 0 end) as hfrom T1