Solved

Master detail query

Posted on 2014-09-10
5
307 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
[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
5 Comments
 
LVL 49

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 49

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

756 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