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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:57:22+00:00 2026-05-15T02:57:22+00:00

I have two tables in a master-detail relationship. The structure is more or less

  • 0

I have two tables in a master-detail relationship.
The structure is more or less as follows:
Master table:

MasterID, DetailID, date, ...
masterID1, detailID1, 2010/5/1, ....
masterID2, detailID1, 2008/6/14, ...
masterID3, detailID1, 2009/5/25, ...
masterID4, detailID2, 2008/7/24, ...
masterID5, detailID2, 2010/4/1, ...
masterID6, detailID4, 2008/9/16, ...

Details table:

DetailID, ...
detailID1, ...
detailID2, ...
detailID3, ...
detailID4, ...

I need to get all the records from the details table plus the LAST record from the master table (last by the date in the master table).
Like this:

detailID1, masterID1, 2010/5/1, ....
detailID2, masterID5, 2010/4/1, ...
detailID3, null, null, ...
detailID4, masterID6, 2008/9/16, ...

I have no idea how to do this.
Can anybody help me?
Thanks a lot.
Jan

  • 1 1 Answer
  • 1 View
  • 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-15T02:57:22+00:00Added an answer on May 15, 2026 at 2:57 am
    Select ....
    From Details As D
        Left Join   (
                    Select M1.MasterId, M.DetailId, M.Date...
                    From Master As M1
                    Where MasterId  = (
                                        Select Max(M2.MasterId)
                                        From Master As M2
                                        Where M2.DetailId = M1.DetailId
                                        Group By M2.DetailId
                                        Having Max(M2.Date) = M1.Date
                                        )
                    ) As M
            On M.DetailId = D.DetailId
    

    What I’m doing here is dealing with ties. If you had two Master rows with the same DetailId and Date, I’m choosing the one with the highest MasterId.

    Btw, this problem is significantly simpler if you have common table expressions (CTE). With a CTE you can do something like:

    With LastMasterRows As
                    (
                    Select MasterId, DetailId, Date
                        , Row_Number() Over( Partition By DetailId Order By Date, MasterId ) As ItemRank
                    From Master
                    )
    Select ...
    From Details As D
        Left Join LastMasterRows As M
            On M.DetailId = D.DetailId
                And M.ItemRank = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: P and PC (master/detail joined by the column Id) Table
I have two tables Master Detail In the Master & Detail table I get
I have two tables Master Detail In the Master & Detail table, i have
I have two large tables -Master table A: 1.4million rows -Detail table B: 9
I have a Master-Detail Relationship between two Tables , I Select a Record in
I've two master tables Org & Item and then there's an OrgItem table.I have
In my display I have two tables in master detail relation. (When I click
I have a two tables as follows: product category(t1): id name product master(t2): id
I have two tables master and detail. When I add a new record to
I have two tables: OutputPackages (master) |PackageID| OutputItems (detail) |ItemID|PackageID| OutputItems has an index

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.