Solved

Master detail query

Posted on 2014-09-10
5
359 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 51

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 51

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

636 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