?
Solved

Picking latest entry to join to

Posted on 2014-10-29
4
Medium Priority
?
128 Views
Last Modified: 2014-10-30
Hi All,

Quite a simple one hopefully.

I am joining to a table that has a number of entries for each ID. All info is the same for each ID, except [date] and [val]. In my join condition, I want to return all data relating to the most recent entry.

What's the best way of doing this?

Thanks
0
Comment
Question by:James Elliott
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 40410576
<total air code for a two-line question.  let me know if this works.>
SELECT st.id, st.goo, st.foo, st.boo, mt.max_date
FROM some_table st
   JOIN (SELECT id, max(date) as max_date from max_table GROUP BY id) mt ON st.id = mt.id

Open in new window


Also, I have an article out there called SQL Server GROUP BY Solutions that has lots of examples of how to join with/to aggregated data.
0
 
LVL 8

Assisted Solution

by:johny_bravo1
johny_bravo1 earned 1000 total points
ID: 40410609
Not sure what exactly you are trying to achieve, but
you can use CROSS APPLY for joining latest record. If you can share your db structure with sample data, it might be useful. Although you can check CROSS APPLY
0
 
LVL 12

Author Closing Comment

by:James Elliott
ID: 40413056
Thanks both.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40413192
Thanks for the split.  For future reference though, more details provided in the original question, to include a data mockup of before / after = more precise answers we can give.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question