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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:46:21+00:00 2026-05-11T15:46:21+00:00

I have a database which tries to acheive point-in-time information by having a master

  • 0

I have a database which tries to acheive point-in-time information by having a master table and a history table which records when fields in the other table will/did change. e.g.

Table: Employee

Id  | Name      | Department ----------------------------- 0   | Alice     | 1 1   | Bob       | 1 

Table: History

ChangeDate   | Field      | RowId  | NewValue     --------------------------------------------- 05/05/2009   | Department | 0      | 2 

That records that employee 0 (Alice) will move to department 2 on 05/05/2009.

I want to write a query to determine the employee’s department on a particular date. So it needs to:

  • Find the first history record for that field and employee before given date
  • If none exists then default to the value currently in the master employee table.

How can I do this? My intuition is to select the first row of a result set which has all suitable history records reverse ordered by date and with the value in the master table last (so it’s only the first result if there are no suitable history records), but I don’t have the required SQL-fu to achieve this.


Note: I am conscious that this may not be the best way to implement this system – I am not able to change this in the short term – though if you can suggest a better way to implement this I’d be glad to hear it.

  • 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. 2026-05-11T15:46:22+00:00Added an answer on May 11, 2026 at 3:46 pm
    SELECT  COALESCE (         (         SELECT  newValue         FROM    history         WHERE   field = 'Department'                 AND rowID = ID                 AND changeDate =                 (                 SELECT MAX(changedate)                 FROM   history                 WHERE  field = 'Department'                        AND rowID = ID                        AND changeDate <= '01/01/2009'                 )         ), department) FROM    employee WHERE   id = @id 

    In both Oracle and MS SQL, you can also use this:

    SELECT  COALESCE(newValue, department) FROM    (         SELECT  e.*, h.*,                 ROW_NUMBER() OVER (PARTITION BY e.id ORDER BY changeDate) AS rn         FROM    employee e         LEFT OUTER JOIN                 history h         ON      field = 'Department'                 AND rowID = ID                 AND changeDate <= '01/01/2009'         WHERE   e.id = @id         ) WHERE rn = 1 

    Note, though, that ROWID is reserved word in Oracle, so you’ll need to rename this column when porting.

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

Sidebar

Related Questions

I have a database which stores (among other things), the following pieces of information:
I have one database table which contains 8 columns. One of the columns is
I have a MySQL database which contains a table of users. The primary key
I have this database which contains a varchar. I want to know which records
I have a MySQL database in which table A has a one-to-many relation to
I have set of records in a database table lets call it Components Table
I have two tables with hierarchyid fields, one of which is a staging table
I have a database which I regularly need to import large amounts of data
I have a database which contains picture data stored as a binary blob. The
I have a database which needs to store year ranges (such as lifespan) which

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.