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

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SHOW INDEX FROM <tablename> You want the row where Key_name… May 11, 2026 at 7:16 pm
  • Editorial Team
    Editorial Team added an answer The big question is: do you have any kind of… May 11, 2026 at 7:16 pm
  • Editorial Team
    Editorial Team added an answer It seems that one solution that would work on any… May 11, 2026 at 7:16 pm

Related Questions

I have a python script which is querying a MySQL server on a shared
I am trying to build SQL for a parameter query in C# for a
I need to test a JDBC connection to a database. The java code to
I am running a couple of databases on MySQL 5.0.45 and am trying to
I'm having a problem getting access to a database which lives on a remote

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.