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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:45:07+00:00 2026-05-11T21:45:07+00:00

Given 2 tables called table1 and table1_hist that structurally resemble this: TABLE1 id status

  • 0

Given 2 tables called “table1” and “table1_hist” that structurally resemble this:

TABLE1
id  status  date_this_status
1   open    2008-12-12
2   closed  2009-01-01
3   pending 2009-05-05
4   pending 2009-05-06
5   open    2009-06-01


TABLE1_hist
id  status  date_this_status
2   open    2008-12-24
2   pending 2008-12-26
3   open    2009-04-24
4   open    2009-05-04

With table1 being the current status and table1_hist being a history table of table1, how can I return the rows for each id that has the earliest date. In other words, for each id, I need to know it’s earliest status and date.

EXAMPLE:

For id 1 earliest status and date is open and 2008-12-12.
For id 2 earliest status and date is open and 2008-12-24.

I’ve tried using MIN(datetime), unions, dynamic SQL, etc. I’ve just reached tsql writers block today and I’m stuck.

Edited to add: Ugh. This is for a SQL2000 database, so Alex Martelli’s answer won’t work. ROW_NUMBER wasn’t introduced until SQL2005.

  • 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-05-11T21:45:08+00:00Added an answer on May 11, 2026 at 9:45 pm

    The following code sample is completely self-sufficient, just copy and paste it into a management studio query and hit F5 =)

    DECLARE @TABLE1 TABLE
            (
            id                  INT,
            status              VARCHAR(50),
            date_this_status    DATETIME
            )
    
    DECLARE @TABLE1_hist TABLE
            (
            id                  INT,
            status              VARCHAR(50),
            date_this_status    DATETIME
            )
    
    --TABLE1
    INSERT  @TABLE1
    SELECT  1,  'open',     '2008-12-12'    UNION ALL
    SELECT  2,  'closed',   '2009-01-01'    UNION ALL
    SELECT  3,  'pending',  '2009-05-05'    UNION ALL
    SELECT  4,  'pending',  '2009-05-06'    UNION ALL
    SELECT  5,  'open',     '2009-06-01'
    
    --TABLE1_hist
    INSERT  @TABLE1_hist
    SELECT  2,  'open',     '2008-12-24'    UNION ALL
    SELECT  2,  'pending',  '2008-12-26'    UNION ALL
    SELECT  3,  'open',     '2009-04-24'    UNION ALL
    SELECT  4,  'open',     '2009-05-04'
    
    SELECT      x.id,
                ISNULL(y.[status], x.[status])                  AS [status],
                ISNULL(y.date_this_status, x.date_this_status)  AS date_this_status
    FROM        @TABLE1 x
    LEFT JOIN   (
                SELECT      a.*
                FROM        @TABLE1_hist a
                INNER JOIN  (
                            SELECT      id,
                                        MIN(date_this_status) AS date_this_status
                            FROM        @TABLE1_hist
                            GROUP BY    id
                            ) b
                        ON  a.id = b.id
                        AND a.date_this_status = b.date_this_status
                ) y
            ON  x.id = y.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need another setter. private string activeFlag { get {… May 12, 2026 at 4:20 pm
  • Editorial Team
    Editorial Team added an answer window.location.href = "someurl"; May 12, 2026 at 4:20 pm
  • Editorial Team
    Editorial Team added an answer You can't do IN this way, because you can't bind… May 12, 2026 at 4:20 pm

Related Questions

I've been beating my brains in over this and I'm making no progress. I'm
I have an enum called Permissions. A user can be assigned permissions, or permissions
I have written an importer which copies data from a flat table into several
I've read a few questions on SO (such as this one ) in regards

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.