Solved

Master detail query

Posted on 2014-09-10
5
325 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 50

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 50

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

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. …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
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.

710 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