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 7815091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:24:16+00:00 2026-06-02T05:24:16+00:00

SQL novice here. I’m trying to generate a costing query that outputs employee time

  • 0

SQL novice here. I’m trying to generate a costing query that outputs employee time card information and calculates cost based on an effective employee costing rate.

My question is similar to the one asked here: Retro-active effective date changes with overlapping dates but I’m not dealing with retro-activity or overlapping date ranges.

Table examples (null values in the rate table indicate current rate):

CREATE TABLE Emp_Rate
(
    Emp int,
    Rate money,
    Rate_Start datetime,
    Rate_Exp datetime
)

CREATE TABLE Emp_Time
(
    Emp int,
    Chrg_Date datetime,
    Chrg_Code varchar(10),
    Chrg_Hrs decimal(8, 2)
)

Insert into Emp_Rate (Emp,Rate,Rate_Start,Rate_Exp) Values ('1','20','5/1/09','4/30/10')
Insert into Emp_Rate (Emp,Rate,Rate_Start,Rate_Exp) Values ('1','21','5/1/10','4/30/11')
Insert into Emp_Rate (Emp,Rate,Rate_Start,Rate_Exp) Values ('1','22','5/1/11',NULL)

Insert into Emp_Time (Emp,Chrg_Date,Chrg_Code,Chrg_Hrs) Values ('1','5/10/09','B','8')
Insert into Emp_Time (Emp,Chrg_Date,Chrg_Code,Chrg_Hrs) Values ('1','5/10/10','B','8')
Insert into Emp_Time (Emp,Chrg_Date,Chrg_Code,Chrg_Hrs) Values ('1','5/10/11','B','8')

The query (returns dupes caused by multiple rate entries(obviously)):

Select  Emp_Time.Emp,
        Cast(Emp_Time.Chrg_Date as DATE) as 'Chrg_Date',
        Emp_Time.Chrg_Code,
        Emp_Time.Chrg_Hrs,
        Emp_Rate.Rate,
        Emp_Time.Chrg_Hrs * Emp_Rate.Rate as 'Cost'

From    Emp_Time inner join
        Emp_Rate on Emp_Rate.Emp = Emp_Time.Emp

Order By [Emp],[Chrg_Date]

Desired output:

Emp Chrg_Date   Chrg_Code   Chrg_Hrs    Rate    Cost
1   2009-05-10  B           8.00        20.00   160.00
1   2010-05-10  B           8.00        21.00   168.00
1   2011-05-10  B           8.00        22.00   176.00

I’ve gone around in circles using the Between operator in a sub query to isolate the correct rate based on the charge date, but have not had any luck.

I appreciate any help!

  • 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-02T05:24:18+00:00Added an answer on June 2, 2026 at 5:24 am

    You didn’t specify the DBMS type the answer below is for sql-server. I am sure there are other ways to do this but this way will replace the null Rate_Exp date with the current date.

    Select  et.Emp,
            Cast(et.Chrg_Date as DATEtime) as 'Chrg_Date',
            et.Chrg_Code,
            et.Chrg_Hrs,
            er.Rate,
            et.Chrg_Hrs * er.Rate as 'Cost'
    From  Emp_Time et
    inner join 
    (
        SELECT Emp
            , Rate
            , Rate_Start
            , CASE
                WHEN Rate_Exp is Null
                THEN Convert(varchar(10), getdate(), 101)
                ELSE Rate_Exp
              END as Rate_Exp
        FROM Emp_Rate 
    )er 
        on er.Emp = et.Emp
    WHERE (et.Chrg_Date BETWEEN er.Rate_Start AND er.Rate_Exp)
    Order By et.Emp,et.Chrg_Date
    

    OR use the CASE Statement in your WHERE Clause:

    Select  et.Emp,
            Cast(et.Chrg_Date as DATEtime) as 'Chrg_Date',
            et.Chrg_Code,
            et.Chrg_Hrs,
            er.Rate,
            et.Chrg_Hrs * er.Rate as 'Cost'
    From  Emp_Time et
    inner join Emp_Rate er
        on er.Emp = et.Emp
    WHERE (et.Chrg_Date 
            BETWEEN er.Rate_Start 
                    AND CASE WHEN er.Rate_Exp Is Null 
                THEN  Convert(varchar(10), getdate(), 101)
                ELSE er.Rate_Exp END)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a novice when it comes to SQL and PHP, and I'm trying to
Could you please list some of the bad practices in SQL, that novice people
database/SQL novice here. I have a table with a column called, for example, EmployeeID.
I'm an novice in pgSQL, so I'm executing different sql commends - here is
I have a large T-SQL query (I only put a part here because I
I'm a novice, both here at stack overflow as well as on sql server,
I've installed Linq To Sql Profiler and notice, that i have double query executing
SQL Server novice here. UPDATE dbo.ObjectivesApproved SET dbo.ObjectivesApproved.VAP = 'Y' WHERE ((dbo.Approved.Cri_Group In ('X01'
I apologize for wording as I'm a bit novice. Here's what I'm trying to
I am a SQL novice. My problem is I can't find the infinite loop

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.