请帮助编写SQL查询

时间:2014-02-01 06:30:37

标签: sql oracle

我需要你的帮助来编写两个SQL查询(DB Oracle)来获取数据。两个表都有大量数据,因此也需要处理性能。以下是场景 -

有两个表Department (DEPT)Employee (EMP)。他们有 1(DEPT):M(EMP)的关系。

Department表有列

Row_id, DeptNm, Created_date

Employee表有列

Row_id, EMPName, Emp_num, Par_row_id (FK to DEPT.row_Id), Salary

对于特定的Department,按每个员工的工资降低排序数据并对其进行排名。数据应如下所示:

DeptNm  | EmpNm    | Salary | Rank              
--------------------------------
Finance | Vikram   | 200000 | 1                     
Finance | Uttaam   | 150000 | 2  
Finance | Rajeev   | 100000 | 3  
ITDPPT  | Balaajii | 150000 | 1  
ITDEPT  | Harsha   | 120000 | 2  
ITDEPT  | Weeniji  | 100000 | 3

查询以显示部门的数据最高薪水。数据应为 -

 Dept_Nm   |       EMP_NM |   Salary   
   Finance |   Vikramadit |  2000000  
   ITDEPT  |       Balaji |  1500000 

3 个答案:

答案 0 :(得分:2)

select *
from (
  select dp.deptname,
         emp.empname,
         emp.salary, 
         dense_rank() over (partition by dp.deptname order by emp.salary desc) as rnk
  from employee emp
    join department dp on dp.row_id = emp.par_row_id 
) t
where rnk = 1

答案 1 :(得分:0)

试试这个会帮助你把Rank放在我没有得到你想要的过程你想要动态排名而不是尝试任何“临时”变量。

select D.DeptName,E.EmpName,E.Salary from Employee E
lefe join Department as D on D.Par_Row_Id=E.Row_Id order by E.Salaray DESC

答案 2 :(得分:0)

select D.DeptName,E.EmpName,E.Salary from Employee E
left join Department as D on D.Par_Row_Id=E.Row_Id order by E.Salaray DESC