B
Bill Jaeck
I am looking for a high performance solution that joins tables and returns
some cost information for employees whose rates change over time. Surely
this problem has been solved many times before, but I have yet to come up
with a solution that I am happy with.
I have the following tables:
Table: Employees - contains employee names and their EmployeeIDs.
EmployeeID
Name
1
Jack Jones
2
John Doe
Etc.
Etc.
Table: Hours - For each day that each Employee works, a row is written to
this table which specifies who worked when and for how many hours.
EmployeeID
Date
Hours
1
1/1/2003
2
1
2/2/2003
3
1
3/3/2003
4
2
3
Etc.
Table: Rates - Each Employee works for a different hourly rate, and the
rates change over time. This table contains the rates for each employee over
time. The effective date shows the first day that the rate is in effect. The
rate is in effect for the employee until a new rate with a more recent
EffectiveDate for that employee is entered into the table. So employee 1
worked for $1.00 per hour in January, $2.00 per hour in February, and for
$3.00 ever since March of 2003.
EmployeeID
EffectiveDate
Rate
1
1/1/2003
$1.00
1
2/1/2003
$2.00
1
3/1/2003
$3.00
2
3
Etc.
Here is the problem. I want to create a "Costs" query which combines the
rows in the Hours table with the effective rate for the employee on the date
the hours were worked so that a net cost can be calculated for the work done
each day by the employee, e.g.
Query: Costs
EmployeeID
Date
Hours
Rate
Cost
1
1/1/2003
2
$1.00
$2.00
1
2/1/2003
3
$2.00
$6.00
1
3/1/2003
4
$3.00
$12.00
2
3
etc
My Hours table is BIG. Obviously, the Cost is the Hours X Rate. But looking
up the correct cost for the date in the Rates table is what I am having a
hard time with. I realize that I could write a VBA function to figure out
the rate (e.g. a function for the Rate column called
RateForEmployee(argEmployeeID as Long, argDate as Date) as Currency). But
that function has to build an SQL string and then open a recordset for each
row in my Hours table, and that takes a long time because there are tens of
thousands of rows in my Hours table.
Does anyone know how I could build this Costs query without having to write
a function in VBA to obtain the Rate to use on the date the hours were
worked? I am looking for a high performance solution.
some cost information for employees whose rates change over time. Surely
this problem has been solved many times before, but I have yet to come up
with a solution that I am happy with.
I have the following tables:
Table: Employees - contains employee names and their EmployeeIDs.
EmployeeID
Name
1
Jack Jones
2
John Doe
Etc.
Etc.
Table: Hours - For each day that each Employee works, a row is written to
this table which specifies who worked when and for how many hours.
EmployeeID
Date
Hours
1
1/1/2003
2
1
2/2/2003
3
1
3/3/2003
4
2
3
Etc.
Table: Rates - Each Employee works for a different hourly rate, and the
rates change over time. This table contains the rates for each employee over
time. The effective date shows the first day that the rate is in effect. The
rate is in effect for the employee until a new rate with a more recent
EffectiveDate for that employee is entered into the table. So employee 1
worked for $1.00 per hour in January, $2.00 per hour in February, and for
$3.00 ever since March of 2003.
EmployeeID
EffectiveDate
Rate
1
1/1/2003
$1.00
1
2/1/2003
$2.00
1
3/1/2003
$3.00
2
3
Etc.
Here is the problem. I want to create a "Costs" query which combines the
rows in the Hours table with the effective rate for the employee on the date
the hours were worked so that a net cost can be calculated for the work done
each day by the employee, e.g.
Query: Costs
EmployeeID
Date
Hours
Rate
Cost
1
1/1/2003
2
$1.00
$2.00
1
2/1/2003
3
$2.00
$6.00
1
3/1/2003
4
$3.00
$12.00
2
3
etc
My Hours table is BIG. Obviously, the Cost is the Hours X Rate. But looking
up the correct cost for the date in the Rates table is what I am having a
hard time with. I realize that I could write a VBA function to figure out
the rate (e.g. a function for the Rate column called
RateForEmployee(argEmployeeID as Long, argDate as Date) as Currency). But
that function has to build an SQL string and then open a recordset for each
row in my Hours table, and that takes a long time because there are tens of
thousands of rows in my Hours table.
Does anyone know how I could build this Costs query without having to write
a function in VBA to obtain the Rate to use on the date the hours were
worked? I am looking for a high performance solution.