简单CASE WHEN
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
通常用SQL语句进行求和都会用到sum()聚合函数 若数据库中有不同type的费用,有若干员工,进行求和每种费用的汇总 。最先想到的可能是查出每种费用的和,再与主表关联。这样太过麻烦,用case when就能解决这一问题
select '汇总金额' as name,
sum(case WHEN reim_type='1' THEN reim_money else 0 end) as type1,
sum(case WHEN reim_type='2' THEN reim_money else 0 end) as type2,
sum(case WHEN reim_type='3' THEN reim_money else 0 end) as type3
from oa_reimburse