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
Solved

sql query help getting the correct record

Posted on 2014-11-14
5
81 Views
Last Modified: 2014-12-02
I have 2 table. 1 table keep all base files. The second table keep track of all uploaded files.

I need to pull only latest records for unique docKey.

In reality i need only record 2 and 3

The attached file has a screen shotscreen capture
0
Comment
Question by:erikTsomik
5 Comments
 
LVL 5

Accepted Solution

by:
TONY TAYLOR earned 500 total points
ID: 40443742
SELECT IM.docKey, MU.filename, IM.name, MU.uploadID, MU.UploadedWhen 
FROM 
	InsManual IM WITH (NOLOCK) 
	OUTER APPLY (
		SELECT TOP 1 * 
		FROM InsManualUploads MUS WITH (NOLOCK) 
		WHERE MUS.docKey = IM.docKey 
		ORDER BY MUS.UploadedWhen DESC
	) MU 
ORDER BY IM.docKey

Open in new window

0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40443765
can you explain outer apply how it works
0
 
LVL 5

Expert Comment

by:TONY TAYLOR
ID: 40443791
This is the best resource that I am readily familiar with for that:
http://www.mssqltips.com/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/

It is a handy trick.  CROSS APPLY might also work for you.  Without knowing your data/project intimately, it would be hard to say.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40444587
select dockey, filename, name, uploadid, uploadedwhen from
(select im.dockey, mu.filename, im.name, mu.uploadid, mu.uploadedwhen,
 row_number() over (partition by mu.dockey order by mu.uploadedwhen desc) rn
 from
 insmanual im inner join insmanualuploads mu
 on im.dockey = mu.dockey) as x
where x.rn = 1
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40446745
Should be as simple as:
SELECT IM.docKey, MU.filename, IM.name, MAX(MU.UploadedWhen)
FROM InsManual IM 
INNER JOIN InsManualUploads MUS ON MUS.docKey = IM.docKey 
GROUP BY IM.docKey, MU.filename, IM.name
ORDER BY IM.docKey

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

840 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