Solved

sql query help getting the correct record

Posted on 2014-11-14
5
84 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
[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 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 50

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

696 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