当前位置: 代码迷 >> Sql Server >> 问个 简略的 sql 条件
  详细解决方案

问个 简略的 sql 条件

热度:376   发布时间:2016-04-24 19:43:23.0
问个 简单的 sql 条件
表t1有8列(a,b,c,d,e,f,g,h)。
前7列如果有两列不为空 则对 h 列求和,这样的sql语句应该怎样写比较简便?
难道要
select sum(h) from t1
where (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 h
from T1

将条件放在后面也是一样的。也就是说一定要将条件一个个写出来吗?没有别的办法啦?


要不就动态语句,但逻辑也是一样的。
  相关解决方案