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)
 Rate Lookup Join

Author  Topic 

rwaite
Starting Member

2 Posts

Posted - 2002-10-22 : 16:08:29
I need to select the appropriate rate to apply to hours worked on a given date and calculate an amount. I've got the following tables:

DateWork
Employee #Hours DateWorked
Jane Doe 8 11/1/02
Jane Doe 8 12/1/02
Jane Doe 8 2/1/03

EmpRate
Employee HourlyRate EffectiveDate
Jane Doe $9 10/1/01
Jane Doe $10 10/1/02
Jane Doe $11 1/1/03

My result should look like this:
Employee DateWorked Amount
Jane Doe 11/1/02 $72
Jane Doe 12/1/02 $80
Jane Doe 2/1/03 $88

I can't figure out how to put together a join to perform this task.

TIA.




robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-10-22 : 16:21:55
SELECT DW.Employee, DW.DateWorked, DW.[#Hours]*ER.HourlyRate AS Amount
FROM DateWork DW INNER JOIN EmpRate ER ON DW.Employee=ER.Employee
INNER JOIN
(SELECT D.Employee, D.DateWorked, Max(E.EffectiveDate) AS MaxDate
FROM DateWork D INNER JOIN EmpRate E ON D.Employee=E.Employee
WHERE D.DateWorked<=E.EffectiveDate
GROUP BY D.Employee, D.DateWorked) A
ON DW.Employee=A.Employee AND DW.DateWorked=A.DateWorked AND ER.EffectiveDate=A.MaxDate


I'm pretty sure that'll do it, and I'm also pretty sure there's a more efficient way to do it, at least one that's a little shorter than this.

Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2002-10-22 : 16:28:46
Here is the original question
http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=20598

Go to Top of Page
   

- Advertisement -