日期:2014-05-17 浏览次数:20806 次
select ID 部门编号, Department 部门名称, Sum ( case when EmployeeType = ' 正式 ' then Cnt else 0 end ) 正式, Sum ( case when EmployeeType = ' 临时 ' then Cnt else 0 end ) 临时 , Sum ( case when EmployeeType = ' 辞退 ' then Cnt else 0 end ) 辞退, Sum ( case when EmployeeType <> '' then Cnt else 0 end ) 合计 from VDepartmentEmployeeType group by ID, Department
------解决方案--------------------
---------- 方法一:case when 语句--------------------- select ID as '部门编号', Department as '部门名称', Sum( case when EmployeeType = ' 正式 ' then Cnt else 0 end ) as "正式", Sum( case when EmployeeType = ' 临时 ' then Cnt else 0 end ) as "临时", Sum( case when EmployeeType = ' 辞退 ' then Cnt else 0 end ) as "辞退", Sum( case when nvl(EmployeeType,'')<>'' then Cnt else 0 end ) as "合计" from VDepartmentEmployeeType group by ID, Department; ---------- 方法二:decode函数 ----------------------- select ID as '部门编号', Department as '部门名称', Sum(decode(EmployeeType,' 正式 ',Cnt,0) as "正式", Sum(decode(EmployeeType,' 临时 ',Cnt,0) as "临时", Sum(decode(EmployeeType,' 辞退 ',Cnt,0) as "辞退", Sum(decode(EmployeeType,null,0,'',0,Cnt) as "合计" from VDepartmentEmployeeType group by ID, Department;
------解决方案--------------------
把case换成decode
select ID as '部门编号', Department as '部门名称',
[正式] = Sum ( decode(EmployeeType,'正式',1,0),
[临时] = Sum (decode(EmployeeType,'临时',1,0 ),
[辞退] = Sum ( decode(EmployeeType,'辞退',1,0 ),
[合计] = Sum ( decode(EmployeeType,'合计',1,0) )
from VDepartmentEmployeeType group by ID, Department
------解决方案--------------------