Oracle数据库
SQL中经常使用sum函数和case when条件判断
case when中是可以加多个条件的
name 多条件时要注意 then的条件
直观代码举例
partake_Num 要求和的字段
COR_STATUS 需要判断的字段
需要计算 COR_STATUS =1 的时候 partake_Num 字段的 和
错误写法:
select sum(case when t.COR_STATUS = 1 then 1 else 0 end) as times,
sum (case when partake_Num !=0 and COR_STATUS = 1 then 1 else 0 end) as pooples
from T_BUSI_DRILL_RECORD t
then 1 的话 这就是统计partake_Num有多少行了 而不是计算 partake_Num的 和
正确写法:
select sum(case when t.COR_STATUS = 1 then 1 else 0 end) as times,
sum (case when partake_Num !=0 and COR_STATUS = 1 then partake_Num else 0 end) as pooples
from T_BUSI_DRILL_RECORD t
要注意 then后面是可以加条件的