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

The Archive Base Latest Questions

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

I have two tables: TableA ( ID [int, pk], Name [string]) and TableB (ID

  • 0

I have two tables: TableA (ID [int, pk], Name [string]) and TableB (ID [int, pk], TableA_ID [int, fk], Name [string], DateStamp [datetime (dd/mm/yyyy hh:MM:ss)]). There is a one-to-many relationship between TableA and TableB.

An inner join on the two tables will give me the following results:

 TableA.ID, TableA.Name, TableB.Name, TableB.DateStamp         1,    'File A', 'Version 1', 01/01/2009 15:00:00         1,    'File A', 'Version 2', 05/01/2009 08:15:00         1,    'File A', 'Version 3', 06/01/2009 19:33:00         2,    'File B', 'Version 1', 03/01/2009 09:10:00         2,    'File B', 'Version 2', 20/01/2009 20:00:00         3,    'File C', 'Version 1', 01/01/2009 17:00:00 

What I actually want is the following (each row from TableA and the last matching row from TableB):

 TableA.ID, TableA.Name, TableB.Name, TableB.DateStamp         1,    'File A', 'Version 3', 06/01/2009 19:33:00         2,    'File B', 'Version 2', 20/01/2009 20:00:00         3,    'File C', 'Version 1', 01/01/2009 17:00:00 

This is the query that I’m using to achieve this:

SELECT ta.ID, ta.Name, tb.Name, tb.DateStamp FROM TableA ta INNER JOIN TableB tb ON ta.ID = tb.TableA_ID WHERE tb.ID IN (  SELECT TOP 1 tb2.ID   FROM TableB tb2   WHERE tb2.TableA_ID = ta.ID   ORDER BY tb2.DateStamp DESC) 

That works but my gut feeling is that I’m not doing this in the ‘best way’. It looks like it is a candidate for an aggregation query (i.e. groupby) but I didn’t have any luck with that. In the end I always had to use a subquery to get the row I’m after in TableB.

Any help much appreciated.

  • 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-11T07:21:24+00:00Added an answer on May 11, 2026 at 7:21 am

    No, there is no requirement to do GROUP BY here, this should be solved through a correlated sub-query:

    SELECT   TableA.ID,    TableA.Name,    TableB.Name,    TableB.DateStamp FROM   TableA   INNER JOIN TableB ON      TableA.ID = TableB.TableA_ID     AND TableB.DateStamp = (       SELECT MAX(DateStamp)        FROM TableB       WHERE TableA_ID = TableA.ID     ) 

    An additional GROUP BY is only necessary if you have more than one record in TableB with equal TableA_ID and equal DateStamp.


    For the specific example you’ve shown, a GROUP BY query happens to produce the correct result. It’s still wrong, because the correct result is more a side-effect in this situation.

    SELECT   TableA.ID,    TableA.Name,    MAX(TableB.Name) Max_TableBName,    MAX(TableB.DateStamp) Max_TableBDateStamp FROM   TableA   INNER JOIN TableB ON TableA.ID = TableB.TableA_ID GROUP BY   TableA.ID,    TableA.Name 

    This relies on the coincidence that MAX(TableB.Name) is in fact the value you want to get out, and it is aligned with MAX(TableB.DateStamp). But since this correlation is a mere accident, the GROUP BY query is wrong.

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

Sidebar

Ask A Question

Stats

  • Questions 86k
  • Answers 86k
  • 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 The Entity Data Source appears on the Toolbox, in the… May 11, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer Trying to clear out my unanswered questions that I've answered… May 11, 2026 at 5:19 pm
  • Editorial Team
    Editorial Team added an answer NLogViewer May 11, 2026 at 5:19 pm

Related Questions

Well I run a small video website and on the actual video page there
Is there a way to merge two primary keys into one and then cascade
I was given a database design which stores information about an organization and any
I have a set of tables that's actually a stubby tree. At the top,

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.