Solved

Master detail query

Posted on 2014-09-10
5
285 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 48

Expert Comment

by:Vitor Montalvão
ID: 40314315
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
ID: 40314332
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
ID: 40323873
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 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40324755
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
ID: 40336352
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
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…

808 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