日期:2014-05-17  浏览次数:20596 次

问个 简单的 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

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


要不就动态语句,但逻辑也是一样的。