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 |
ctlqt12
Starting Member
2 Posts |
Posted - 2006-08-20 : 06:37:42
|
Hi, i have two tables follow :tblEMP(empID,empNAME,empROOM).empID empNAME empROOM1 John C#2 Peter ASP3 Otto C#4 Adams C++5 Henry VB6 Potter C++tblDETAIL(empID,Salary).empID Salary1 3002 3503 3204 3105 3206 330Problem :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# 3202 Peter ASP 3506 Potter C++ 3305 Henry VB 320Please 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 FOn F.EmpID = E.EmpID [/code]Chirag |
 |
|
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)ASSELECT E.empID, E.empNAME, E.empROOM, D.SalaryFROM tblEMP AS EINNER JOIN tblDETAIL AS D ON E.empID = D.empIDGOSELECT empID, empNAME, empROOM, SalaryFROM EmployeeWithSalary AS E1WHERE Salary = ( SELECT MAX(E2.Salary) FROM EmployeeWithSalary AS E2 WHERE E1.empROOM = E2.empROOM ) |
 |
|
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 @TblDetailSelect 1,300 Union AllSelect 2,350 Union AllSelect 3,320 Union AllSelect 4,310 Union AllSelect 5,320 Union AllSelect 6,330Select 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 ont.empid = t1.empid Group by t1.EmpRoom) As FOn f.EmpRoom = E.EmpRoom And F.MaxSalary= e.Salaryorder by MaxSalary Chirag |
 |
|
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? |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-08-20 : 20:43:03
|
Outstanding NZDF Arnold!CODO ERGO SUM |
 |
|
|
|
|
|
|