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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:57:39+00:00 2026-05-13T23:57:39+00:00

Scenario: Table A MasterID, Added Date, Added By, Updated Date, Updated By, 1, 1/1/2010,

  • 0

Scenario:

Table A
MasterID, Added Date, Added By, Updated Date, Updated By,
1, 1/1/2010, ‘Fred’, null, null
2, 1/2/2010, ‘Barney’, ‘Mr. Slate’, 1/7/2010
3, 1/3/2010, ‘Noname’, null, null

Table B
MasterID, Added Date, Added By, Updated Date, Updated By,
1, 1/3/2010, ‘Wilma’, ‘The Great Kazoo’, 1/5/2010
2, 1/4/2010, ‘Betty’, ‘Dino’, 1/4/2010

Table C
MasterID, Added Date, Added By, Updated Date, Updated By,
1, 1/5/2010, ‘Pebbles’, null, null
2, 1/6/2010, ‘BamBam’, null, null

Table D
MasterID, Added Date, Added By, Updated Date, Updated By,
1, 1/2/2010, ‘Noname’, null, null
3, 1/4/2010, ‘Wilma’, null, null

I need to return the max added date and corresponding user, and max updated date and corresponding user for each distinct record when tables A,B,C&D are UNION’ed, i.e.:

1, 1/5/2010, ‘Pebbles’, ‘The Great Kazoo’, 1/5/2010
2, 1/6/2010, ‘BamBam’, ‘Mr. Slate’, 1/7/2010
3, 1/4/2010, ‘Wilma’, null, null

I know how to do this with one date/user per row, but with two is beyond me.
DBMS is SQL Server 2005. T-SQL solution preferred.

Thanks in advance,
Dave

  • 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-13T23:57:39+00:00Added an answer on May 13, 2026 at 11:57 pm

    Do as you would with one date/user per row, and repeat for modified date, then join the two resulting tables together on the MasterID.

    SELECT added.MasterID, added.AddedDate, added.AddedBy, modif.UpdatedDate, modif.UpdatedBy FROM
    (
    SELECT a.MasterID, a.AddedDate, b.AddedBy FROM
    (
        SELECT MasterID, Max(AddedDate) As AddedDate FROM
        (
            SELECT * FROM TableA
            UNION ALL
            SELECT * FROM TableB
            UNION ALL
            SELECT * FROM TableC
            UNION ALL
            SELECT * FROM TableD
        )
        GROUP BY MasterID
    ) a
    JOIN
    (
            SELECT * FROM TableA
            UNION ALL
            SELECT * FROM TableB
            UNION ALL
            SELECT * FROM TableC
            UNION ALL
            SELECT * FROM TableD
    ) b
    ON a.MasterID = b.MasterID AND a.AddedDate = b.AddedDate
    ) added
    LEFT OUTER JOIN
    (
    SELECT a.MasterID, a.UpdatedDate, b.UpdatedBy FROM
    (
        SELECT MasterID, Max(UpdatedDate) As UpdatedDate FROM
        (
            SELECT * FROM TableA
            UNION ALL
            SELECT * FROM TableB
            UNION ALL
            SELECT * FROM TableC
            UNION ALL
            SELECT * FROM TableD
        )
        GROUP BY MasterID
    ) a
    JOIN
    (
            SELECT * FROM TableA
            UNION ALL
            SELECT * FROM TableB
            UNION ALL
            SELECT * FROM TableC
            UNION ALL
            SELECT * FROM TableD
    ) b
    ON a.MasterID = b.MasterID AND a.UpdatedDate = b.UpdatedDate
    ) modif
    ON added.MasterID = modif.MasterID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a simple scenario with table characters: CharacterName GameTime Gold Live Foo 10
Here's my scenario: I've got a table of (let's call them) nodes. Primary key
I am using Spring and JDBCTemplate. The scenario is a CUSTOMER table and ORDERS
Here is my scenario Table 1: GID || Info1 Table 2: GID || Tb1GID
I wonder how JPA defines to handle following scenario: Table A: | Table B:
I have a scenario where I would like to select rows from a table
Examine the following scenario. There is a table with two columns - DateWithoutMilliseconds and
I have a bit of an odd scenario where I have a table that
I have a simple (hopefully) scenario. Seats table Computers table SeatComputers table (since a
I have this scenario: Table A: --------------- ID| SOME_VALUE| --------------- 1 | 123223 |

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.