Solved

sql query help getting the correct record

Posted on 2014-11-14
5
83 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CAST issue with SQL 2000 15 44
Complex SQL Server WHERE CLause 9 39
Using a SUBQUERY for the set variable 10 27
Failover Cluster Primary Nodes Current Vote = 0 5 35
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

734 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