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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:23:06+00:00 2026-06-06T10:23:06+00:00

So I have a reference table which stores the primary key, description and update

  • 0

So I have a reference table which stores the primary key, description and update date columns. Something like this

SELECT * FROM tblReasonRef

ReasonCode   Description   UpdateDate
27           Lunch         2010-12-01
24           Meeting       2010-12-01
20           SpecialProj   2010-12-01

The other day, the code description was changed. So now the query returns the following…

ReasonCode   Description   UpdateDate
27           Lunch         2010-12-01
24           Meeting       2010-12-01
20           SpecialProj   2010-12-01
27           Training      2012-06-22
24           Meeting       2012-06-22
20           Lunch         2012-06-22

The source data tracks every 30 minutes what state a staff member might go into, so you would have the following query…

SELECT * FROM tblhActivity

MemberID     Date         Time    ReasonCode    ReasonDuration
10922        2012-06-21   1200    27            100
10922        2012-06-21   1500    24            1800
10922        2012-06-25   1230    27            100

So originally, the query I had was…

SELECT a.MemberID, a.Date, a.Time, r.Description, a.ReasonDuration 
  FROM tblhActivity a
 INNER JOIN tblReasonRef r ON a.ReasonCode = r.ReasonCode

Which worked fine until the change on the 22nd. Now I have two definitions of each code. The question is, how can create a query that will pick the right code depending on the date.

For example, I know that when the date is the 21st, the description for code 27 should be lunch. On the 25th, the description returned should be Training.

Keep in mind also, that this will probably happen again where codes are added to the reference table. I am trying to think the join should also be on UpdateDate but I have to know the start and end date of each reference code. Is there a simple solution?

  • 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-06T10:23:08+00:00Added an answer on June 6, 2026 at 10:23 am

    You really need the start and end dates for the period in which a particular reason is applicable. You can either modify your tblReasonRef to include these dates (best option) or you will need to calculate them.

    The following query will calculate an end date for each reason as the day before a new entry for the ReasonCode is added.

    SELECT ReasonCode
        ,Description
        ,UpdateDate StartDate
        ,DATEADD(d, -1, UpdateDate) PreviousEntryEndDate
        ,ROW_NUMBER() OVER(PARTITION BY ReasonCode ORDER BY UpdateDate) AS Row 
    INTO #reason
    FROM tblReasonRef
    
    SELECT a.MemberID
        ,a.Date
        ,a.Time
        ,reason.ReasonCode
        ,a.ReasonDuration
    FROM tblhActivity a
        INNER JOIN #reason reason
            ON a.ReasonCode = reason.ReasonCode
        LEFT JOIN #reason nextReason
            ON reason.Row = nextReason.Row - 1
            AND reason.ReasonCode = nextReason.ReasonCode
    WHERE a.Date BETWEEN reason.StartDate AND ISNULL(nextReason.PreviousEntryEndDate, a.Date)
    
    DROP TABLE #reason
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing primary key and foreign key that references same table.
I have a table that has a foreign key to a table that stores
I have an invoices table which stores a single record for each invoice, with
Most databases have something like a GREATEST function, which can be useful some times.
I have a table A which references table B. edit: Database engine used is
Hi I have a table which references itself and I need to be able
I have a simple three table reference. It looks plain enough. The associations there
I have four different tables, one main SPECIAL table containing only id's that reference
I have a bidding table, as follows: SellID INT FOREIGN KEY REFERENCES SellItem(SellID), CusID
I have an EntiyFramework project for which i am storing settings in a table.

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.