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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 difficult select

Author  Topic 

ctlqt12
Starting Member

2 Posts

Posted - 2006-08-20 : 06:37:42
Hi, i have two tables follow :
tblEMP(empID,empNAME,empROOM).
empID empNAME empROOM
1 John C#
2 Peter ASP
3 Otto C#
4 Adams C++
5 Henry VB
6 Potter C++

tblDETAIL(empID,Salary).
empID Salary
1 300
2 350
3 320
4 310
5 320
6 330

Problem :
How could i write a query ( use MSSQL Server 2000 and don't use VIEW ) to display the empID,empNAME,empROOM and the highest Salary of each ROOM?

The result should be :
3 Otto C# 320
2 Peter ASP 350
6 Potter C++ 330
5 Henry VB 320


Please help me!

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-20 : 06:44:04
[code]
Select E.EmpID,E.EmpName,E.EmpRoom,F.MaxSalary From TblEmp as E
Inner Join
(Select Max(Salary) MaxSalary ,EmpID From TblDetail Group by EmpID) As F
On F.EmpID = E.EmpID
[/code]

Chirag
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2006-08-20 : 07:28:03
Your tables are poorly specified. What are the primary keys of tblEMP and tblDETAIL? If empID is the primary key of both, why is tblDETAIL a separate table at all? Are there employees that we don't know the salary of?

Your desired result doesn't match the most obvious interpretation of the prose description of the problem "display the empID,empNAME,empROOM and the highest Salary of each ROOM". Presumably the prose is just badly worded.

The problem is underspecified: it does not consider what the result should be if the highest salary in a room is earned by more than one employee.

I wonder why your instructor told you not to use a view for this exercise: since you need to join tblEMP and tblDETAIL to get the highest salary for each room, and then join them again to get the person (or people) with that salary, the sensible way would be to create a view:


CREATE VIEW EmployeeWithSalary (empID, empNAME, empROOM, Salary)
AS
SELECT E.empID, E.empNAME, E.empROOM, D.Salary
FROM tblEMP AS E
INNER JOIN tblDETAIL AS D ON E.empID = D.empID
GO

SELECT empID, empNAME, empROOM, Salary
FROM EmployeeWithSalary AS E1
WHERE Salary = (
SELECT MAX(E2.Salary)
FROM EmployeeWithSalary AS E2
WHERE E1.empROOM = E2.empROOM
)

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-20 : 07:57:54
Or May be somthing like this??


Declare @tblEMP Table
(
empID int,
empNAME varchar(20),
empROOM varchar(10)
)

Insert @TblEmp
Select 1,'John', 'C#' Union All
Select 2,'Peter','ASP' Union All
Select 3,'Otto','C#' Union All
Select 4,'Adams','C++' Union All
Select 5,'Henry','VB' Union All
Select 6,'Potter','C++'

Declare @TblDetail Table
(
Empid int ,
Salary money
)

Insert @TblDetail
Select 1,300 Union All
Select 2,350 Union All
Select 3,320 Union All
Select 4,310 Union All
Select 5,320 Union All
Select 6,330




Select E.EmpID,E.EmpName,E.EmpRoom,MaxSalary From
(Select E.EmpID,E.EmpName,E.EmpRoom,F.Salary From @TblEmp As E Inner Join
@TblDetail f On f.empid = e.empid )as E
Inner Join
(Select Max(t.Salary) MaxSalary ,t1.EmpRoom From @TblDetail t Inner Join @TblEmp t1 on
t.empid = t1.empid
Group by t1.EmpRoom) As F
On f.EmpRoom = E.EmpRoom And F.MaxSalary= e.Salary
order by MaxSalary



Chirag
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2006-08-20 : 08:36:31
Yes, that would probably get a better mark.
However, my answer allows the course instructor to start all sorts of interesting discussions in class. E.g. Why are the query plans (for the example data) so different? What is the Segment operator and why isn't it documented for SQL Server 2000? If the estimated cost of the sort-and-segment plan is half that of the join-everything plan, why does SQL Server use that plan? Can't the query optimizer tell when an join is really being used as a semijoin? Do the estimated costs reflect reality? When would sort-and-segment be a bad idea?
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-08-20 : 20:43:03
Outstanding NZDF Arnold!








CODO ERGO SUM
Go to Top of Page
   

- Advertisement -