当前位置: 代码迷 >> Oracle开发 >> SQL Server 改成ORACLE的语法
  详细解决方案

SQL Server 改成ORACLE的语法

热度:312   发布时间:2016-04-24 08:03:49.0
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……
  相关解决方案