Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
madhuvrk
Starting Member
2 Posts |
Posted - 2014-09-14 : 04:00:41
|
Hi,I have the employees and department tables(structure below). Can anyone please help me write a sql to get the department name,employee earning maximum salary,employee earning minimum salary for each departmentresult set:dname |max_salaried_employee|min_salaried_employee------------------------------------------------------Accounts | Blakes | MillerHR | King | JamesStructure :create table dept( deptno number(2,0), dname varchar2(14), loc varchar2(13), constraint pk_dept primary key (deptno)); create table emp( empno number(4,0), ename varchar2(10), job varchar2(9), mgr number(4,0), hiredate date, sal number(7,2), comm number(7,2), deptno number(2,0), constraint pk_emp primary key (empno), constraint fk_deptno foreign key (deptno) references dept (deptno));Any help would be appreciated!!ThanksMadhu |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-09-14 : 07:43:00
|
select * from (select *, row_number() over (partition by deptno order by sal) as rn, count(*) over (partition by deptno) AS c from dbo.emp) AS dwhere rn in (1, c); Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|