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

SQL Server 改为ORACLE的语法
select ID as '部门编号', Department as '部门名称', 
  [正式] = 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

上面的sql是sqlserver的,是个表的行列转换
[正式] = Sum (...),改成oracle应该怎么写呢?

------解决方案--------------------
select ID as '部门编号', Department as '部门名称', 
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
------解决方案--------------------
这个
SQL code
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

------解决方案--------------------
探讨
select ID as '部门编号', Department as '部门名称',
[正式] = Sum ( case when EmployeeType = ' 正式 ' then Cnt else 0 end ),
[临时] = Sum ( case when EmployeeType = ' 临时 ' then Cnt else 0 end ),
[辞退] = Sum……

------解决方案--------------------
SQL code
---------- 方法一: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
------解决方案--------------------
探讨
select ID as '部门编号', Department as '部门名称',
Sum ( case when EmployeeType = ' 正式 ' then Cnt else 0 end ) 正式,
Sum ( case when EmployeeType = ' 临时 ' then Cnt else 0 end ) 临时 ,
Sum ( case when E……