Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:03:05+00:00 2026-06-04T02:03:05+00:00

We have a time management system where our employees or contractors (resources) enter the

  • 0

We have a time management system where our employees or contractors (resources) enter the hours they have worked, and we derive a cost for it. I have a table with the historic costs:

CREATE TABLE ResourceTimeTypeCost (
 ResourceCode VARCHAR(32),
 TimeTypeCode VARCHAR(32),
 EffectiveDate DATETIME,
 CostRate DECIMAL(12,2)
)

So I have one date field which marks the effective date. If we have a record which is

('ResourceA', 'Normal', '2012-04-30', 40.00)

and I add a record which is

('ResourceA', 'Normal', '2012-05-04', 50.00) 

So all hours entered between the 30th April and the 3rd of May will be at £40.00, all time after midnight on the 4th will be at £50.00. I understand this in principle but how do you write a query expressing this logic?

Assuming my time table looks like the below

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 records into 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’d like to get a query that returns the total cost by resource

So in the case above it would be ‘ResourceA’, (4 * 7.5 * 40) + (3 * 7.5 * 50) = 2325.00

Can anyone provide a sample SQL query? I know this example doesn’t make use of TimeType (i.e. it’s always ‘Normal’) but I’d like to see how this is dealt with as well

I can’t change the structure of the database. Many thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:03:06+00:00Added an answer on June 4, 2026 at 2:03 am
    IF OBJECT_ID ('tempdb..#ResourceTimeTypeCost') IS NOT NULL DROP TABLE #ResourceTimeTypeCost
    CREATE TABLE #ResourceTimeTypeCost (  ResourceCode VARCHAR(32),  TimeTypeCode VARCHAR(32),  EffectiveDate DATETIME,  CostRate DECIMAL(12,2) ) 
    INSERT INTO #ResourceTimeTypeCost 
    SELECT 'ResourceA' as resourcecode, 'Normal' as timetypecode, '2012-04-30' as effectivedate, 40.00 as costrate
    UNION ALL
    SELECT 'ResourceA', 'Normal', '2012-05-04', 50.00
    
    IF OBJECT_ID ('tempdb..#TimeEntered') IS NOT NULL DROP TABLE #TimeEntered
    CREATE TABLE #TimeEntered (  ResourceCode VARCHAR(32),  TimeTypeCode VARCHAR(32),  ProjectCode VARCHAR(32),  ActivityCode VARCHAR(32),  TimeEnteredDate DATETIME,  HoursWorked DECIMAL(12,2) ) 
    INSERT INTO #TimeEntered 
    SELECT 'ResourceA','Normal','Project1','Management1','2012-04-30',7.5 
    UNION ALL SELECT 'ResourceA','Normal','Project1','Management1','2012-05-01',7.5 
    UNION ALL SELECT 'ResourceA','Normal','Project1','Management1','2012-05-02',7.5 
    UNION ALL SELECT 'ResourceA','Normal','Project1','Management1','2012-05-03',7.5 
    UNION ALL SELECT 'ResourceA','Normal','Project1','Management1','2012-05-04',7.5 
    UNION ALL SELECT 'ResourceA','Normal','Project1','Management1','2012-05-07',7.5 
    UNION ALL SELECT 'ResourceA','Normal','Project1','Management1','2012-05-08',7.5 
    
    ;with ranges as
    (
    select 
    resourcecode 
    ,TimeTypeCode
    ,EffectiveDate
    ,costrate
    ,row_number() OVER (PARTITION BY resourcecode,timetypecode ORDER BY effectivedate ASC) as row
    from #ResourceTimeTypeCost 
    )
    ,ranges2 AS
    (
    SELECT 
    r1.resourcecode 
    ,r1.TimeTypeCode
    ,r1.EffectiveDate
    ,r1.costrate
    ,r1.effectivedate as start_date
    ,ISNULL(DATEADD(ms,-3,r2.effectivedate),GETDATE()) as end_date
    FROM ranges r1
    LEFT OUTER JOIN ranges r2 on r2.row = r1.row + 1 --joins onto the next date row
                        AND r2.resourcecode = r1.resourcecode 
                        AND r2.TimeTypeCode = r1.TimeTypeCode
    )
    SELECT 
    tee.resourcecode
    ,tee.timetypecode
    ,tee.projectcode
    ,tee.activitycode
    ,SUM(ranges2.costrate * tee.hoursworked) as total_cost
    FROM #TimeEntered tee
    INNER JOIN ranges2 ON tee.TimeEnteredDate >= ranges2.start_date
                        AND tee.TimeEnteredDate <= ranges2.end_date
                        AND tee.resourcecode = ranges2.resourcecode
                        AND tee.timetypecode = ranges2.TimeTypeCode
    GROUP BY tee.resourcecode
    ,tee.timetypecode
    ,tee.projectcode
    ,tee.activitycode
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a content management system to allow our users to store files uploaded
I have a requirement to allow users in a content management system to create
While setting up an online file management system, and now I have hit a
I'm trying to make the domain model of a management system. I have the
Our project is a content management system supporting several dozen of our websites. The
We have a setup a product management system where the head of the product
I have a php browser based application that is for a hospital management system.
We have a web content management system (based on Sharepoint 2007/MOSS, but for the
hi I have doubts about the xcode 4.2 memory management system. I have read
For a long time, we've wanted to create a case management system where no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.