Solved

sql query help getting the correct record

Posted on 2014-11-14
5
77 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
Comment Utility
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
Comment Utility
can you explain outer apply how it works
0
 
LVL 5

Expert Comment

by:TONY TAYLOR
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now