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

  • Home
  • SEARCH
  • 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 8378099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:50:55+00:00 2026-06-09T15:50:55+00:00

MySql 5.5. I have a table that represents a work assignment: empId jobNo workDate

  • 0

MySql 5.5.

I have a table that represents a work assignment:

   empId   jobNo   workDate   hours
     4      441    10/1/2012    10
     4      441     9/1/2012    22
     4      441     8/1/2012     6

And one that represents salary:

   empId   effDate     rate
     4     10/1/2012    6.50
     4      9/1/2012    5.85
     4      6/1/2012    4.00

The salary applies to all work performed on or after the effective date. So the rate in jun, jul, and aug is 4.00; sep is 5.85, and oct is 6.50.

If I naively query for October’s work:

   SELECT Work.empId, Work.jobNo, Work.workDate, Work.hours, Salary.effDate, Salary.rate
   FROM Work
   JOIN Salary ON Work.empId = Salary.empId
   WHERE Work.workDate <= '2012-10-01'
      AND Salary.effDate <= Work.workDate
   ORDER BY Work.jobNo ASC, Work.workDate DESC;

I do not get what I want. I get something like

   4   441   10/1/2012   10   10/1/2012   6.50
   4   441   10/1/2012   10    9/1/2012   5.85
   4   441   10/1/2012   10    6/1/2012   4.00
   4   441    9/1/2012   22    9/1/2012   5.85
   4   441    9/1/2012   22    6/1/2012   4.00
   4   441    8/1/2012    6    6/1/2012   4.00

When I want

   4   441   10/1/2012   10   10/1/2012   6.50
   4   441    9/1/2012   22    9/1/2012   5.85
   4   441    8/1/2012    6    6/1/2012   4.00

I can’t quite wrap my head around how to create the query I want.
The real situation has multiple employees, multiple jobs, obviously.

Thanks for your 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-09T15:50:56+00:00Added an answer on June 9, 2026 at 3:50 pm

    Here is your actual issue: you want to be able to detect, for each record in Work, what is the corresponding effective rate, according to the work date x salary effective date. When you simply do Salary.effDate <= WORK.workDate you get ALL rates before the work date. But you only want the most recent one.

    This is a slightly complicated variant of the greatest-n-per-group problem. There are many ways of doing this, here is one:

    SELECT sel.*, Salary.Rate
    FROM
    (
         SELECT Work.empId, Work.jobNo, Work.workDate, 
               Work.hours, Max(Salary.effDate) effDate
        FROM WORK
        JOIN Salary ON WORK.empId = Salary.empId
        WHERE WORK.workDate <= '2012-10-01'
           AND Salary.effDate <= WORK.workDate
        GROUP BY WORK.empId, WORK.jobNo, WORK.workDate, WORK.hours
        ORDER BY WORK.jobNo ASC, WORK.workDate DESC
    ) sel 
    INNER JOIN Salary ON sel.empId = Salary.empId 
                      AND sel.EffDate = Salary.EffDate
    

    First of all, the inner query detects the most recent salary effective date for each work record. Then, we join that with the Salary again to the rate.

    See the working SQLFiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql table that have several records linked to one particular tag.
I have in a MySQL table a DATE column that represents the date in
I have a MySQL table that represents a list of orders and a related
I have mysql table that has a column that stores xml as a string.
I have a MySQL table that looks something like this: +-----+------------+ | id |
I have a mysql table that looks like this: id col2 col3 col4 1
I have a MySQL table that holds many entries with repeated IDs (for various
I have a MySQL table that defines values for a specific customer type. |
I have a mySQL table that has a column like this: ID ----- 0352
I have a MySQL table that has a field of comma separated values. How

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.