Solved

Master detail query

Posted on 2014-09-10
5
235 Views
Last Modified: 2014-10-01
Hello,

I have two tables: Master and Detail.  

They are joined on a column called master_id

The detail table has an identity column called Id.

I need to write a query that returns all the columns in the master table, plus the columns for the Detail table for the row that has the highest value of Id.  (i.e. the latest row).

Can anyone suggest a general syntax?

To summerize:  A master row plus the detail row that has the highest value of id for any given value of master_id.
0
Comment
Question by:soozh
5 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Should be something like:
SELECT *
FROM Master M
   INNER JOIN Detail D
        ON M.master_id = D.Id
WHERE M.master_id = (SELECT MAX(master_id) FROM Master)

Open in new window

0
 
LVL 8

Expert Comment

by:Ganapathi
Comment Utility
This query will return the Maximum ID row from Detail table for each matching ID row in Master table
SELECT *
FROM
   Master mstr,
   Detail dtl
WHERE
   mstr.ID = dtl.ID
GROUP BY
   dtl.ID
HAVING
   dtl.ID = MAX(dtl.ID)

Open in new window

0
 

Author Comment

by:soozh
Comment Utility
I used:

SELECT *
FROM Master M
   INNER JOIN Detail D
        ON M.master_id = D.Id
WHERE d.id = (SELECT MAX(id) FROM detail)
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
soozh, that's should return the same as my suggestion since there's an INNER JOIN ON M.master_id = D.Id but I still recommend you to use Master in the subselect instead of detail because master_id should be the primary key and Master table should also have less records than detail table.
0
 
LVL 21

Expert Comment

by:Alpesh Patel
Comment Utility
SELECT * FROM (SELECT *, Row_Number()Over (Partition by M.master_id order by M.master_id ASC, d.id DESC) RN
FROM Master M
   INNER JOIN Detail D
        ON M.master_id = D.master_id)
A WHERE RN = 1
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now