views:

53

answers:

2

Suppose i have two tables

declare @emp table
(
  EmpID int,
  EmpName varchar(10)

)

declare @Remu table
(
   EmpID int,
   Sal Decimal(10,2),
   PaidYear varchar(10)
)

I want maximum salary grouped on PaidYear (With Ties)

Expected OUTPUT

EmpID EmpName PaidYear Sal 
1     Jon     2001     2000
2     Smith   2001     2000

3     Nash   2003      4000     
4     Hoge   2005      5000
5     Peter  2005      5000

I have an issue when using Join

select e.EmpID,e.EmpName,r.Sal,r.PaidYear from @emp e
inner join
(select max(Sal) as Sal,PaidYear from @Remu group by PaidYear)r
on e.EmpID=???

when i select EmpID in

select max(Sal) as Sal,PaidYear from @Remu group by PaidYear

i have to Group by PaidYear and EmpID,which won't give the desired result as i expected.

How to solve this.I want a query which should be compatible with SQL Server 2000.

+1  A: 
select e.EmpID,e.EmpName,r.Sal,r.PaidYear
from @emp e inner join @Remu r on e.EmpId = r.EmpId
where r.sal in (select max(sal) from @remu group by paidyear)
Muhammad Kashif Nadeem
This is not correct. You are saying that 'any' salary that happens to be the max in any given year can be considered the max for a specific year.
Jeff O
@Jeff could not get you. If you can explain or give me any example so I can correct myself.
Muhammad Kashif Nadeem
the select statement in you where clause is going to return a list of the largest salary for each year. Example: In year 2000 it could be 3000 and in year 2001 it could be 4000, but if anyone made 3000 in year 2001 they also would show up in your query. You are not limiting it to match the same year. See my answer.
Jeff O
A: 

Each year needs to determine a single max salary specific to that year.

select e.EmpID
    , e.EmpName
    , r.Sal
    , r.PaidYear 
from @emp as e 
inner join @Remu as r 
on e.EmpId = r.EmpId 
where r.sal = (select max(sal) 
               from @remu 
               where paidyear = r.PaidYear ' makes it year specific
              ) 

Data To Test:

declare @emp table 
( 
  EmpID int, 
  EmpName varchar(10) 

) 

declare @Remu table 
( 
   EmpID int, 
   Sal Decimal(10,2), 
   PaidYear varchar(10) 
) 

insert into @emp (EmpID, EmpName)
values(1, 'Jon')
insert into @emp (EmpID, EmpName)
values(2, 'Smith')
insert into @emp (EmpID, EmpName)
values(3, 'Nash')
insert into @emp (EmpID, EmpName)
values(4, 'Hoge')
insert into @emp (EmpID, EmpName)
values(5, 'Peter')

Insert into @Remu (EmpID, Sal, PaidYear)
values(1, 2000, '2001')

Insert into @Remu (EmpID, Sal, PaidYear)
values(2, 4999, '2001') 

Insert into @Remu (EmpID, Sal, PaidYear)
values(2, 8000, '2003') 

Insert into @Remu (EmpID, Sal, PaidYear)
values(3,4000, '2003')      

Insert into @Remu (EmpID, Sal, PaidYear)
values(4, 5000, '2005')      

Insert into @Remu (EmpID, Sal, PaidYear)
values(5, 4999, '2005')

Results:

EmpID   EmpName Sal    PaidYear
4           Hoge    5000.00    2005
2           Smith   8000.00    2003
2           Smith   4999.00    2001
Jeff O
@Jeff O, thanks for showing this to me. But with all respect I don't see any difference because I GROUP BY on PaidYear. I have put some data and run both queries and there is no difference. Can you please show me the difference. It would be so nice of you.
Muhammad Kashif Nadeem
@Muhammad Kashif Nadeem - I've added some code to insert test records. Test your query and you'll find that 'Peter' and 'Hoge' both show up as having max salary in year 2005 eventhough they have different salaries. The reason is the salary for 'Peter' matches a max salary from another year (2001)..
Jeff O
@Jeff O, thanks again. I have executed both, your and mine, query using your data and result is same. I had this exercise earlier and could not find any difference that is why I have asked you that where is the difference in results between your and my query.
Muhammad Kashif Nadeem
Yours returns 4 records (because your query will return 2 records for 2005 with different salaries) and mine returns 3. I've posted the correct results.
Jeff O