We have a time management system when our employees or contractors (resources) enter during business hours, and we get costs for this. I have a table with historical costs:
CREATE TABLE ResourceTimeTypeCost ( ResourceCode VARCHAR(32), TimeTypeCode VARCHAR(32), EffectiveDate DATETIME, CostRate DECIMAL(12,2) )
So, I have one date field that marks the effective date. If we have a record that
('ResourceA', 'Normal', '2012-04-30', 40.00)
and I add a record that
('ResourceA', 'Normal', '2012-05-04', 50.00)
Thus, all hours entered between April 30 and May 3 will be £ 40.00, all the time after midnight on the 4th will be £ 50. I understand this in principle, but how do you write a query expressing this logic?
Assuming my timeline is as follows
CREATE TABLE TimeEntered ( ResourceCode VARCHAR(32), TimeTypeCode VARCHAR(32), ProjectCode VARCHAR(32), ActivityCode VARCHAR(32), TimeEnteredDate DATETIME, HoursWorked DECIMAL(12,2) )
If I insert the following entries in the TimeEntered table
('ResourceA','Normal','Project1','Management1','2012-04-30',7.5) ('ResourceA','Normal','Project1','Management1','2012-05-01',7.5) ('ResourceA','Normal','Project1','Management1','2012-05-02',7.5) ('ResourceA','Normal','Project1','Management1','2012-05-03',7.5) ('ResourceA','Normal','Project1','Management1','2012-05-04',7.5) ('ResourceA','Normal','Project1','Management1','2012-05-07',7.5) ('ResourceA','Normal','Project1','Management1','2012-05-08',7.5)
I would like to receive a request that returns the total cost of resources
So, in the above case, it will be "ResourceA", (4 * 7.5 * 40) + (3 * 7.5 * 50) = 2325.00
Can someone provide a sample SQL query? I know that this example does not use TimeType (that is, it is always "Normal"), but I would like to see how it is also considered
I cannot change the database structure. Thank you very much in advance