Solved

SQL - Update from top 1 in another table

Posted on 2010-11-30
7
366 Views
Last Modified: 2012-05-10
Hello,

I need to Update all values in Tbl1.Col3 (nvarchar)
From Tbl2.Col3
Where Tbl1.Col1 = Tbl2.Col1
And Tbl.Col4 is the most recent (Date Desc)


Tbl1:                                
-------------------------------
SKU     DESC.     STATUS
Abc     None             *
Def      None             **
                                 
                                 
Tbl2:                                                                            
-------------------------------                                          
SKU     DESC.     STATUS     DATE                              
Abc     None       A                20101023 12:03            
Abc     None       B                20101101 8:16              
Def     None       D                20101029 5:15
Def     None       B                  20101028 10:10            

                                                                                   

* This would be Set to 'B'    
** This would be Set to 'D'    
0
Comment
Question by:Rick
[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
7 Comments
 
LVL 13

Author Comment

by:Rick
ID: 34241009
Oops, wrong Zone...
I meant to pick MS SQL Server, not MySQL Server...
0
 
LVL 6

Expert Comment

by:hyphenpipe
ID: 34241051
with updateCTE
as
(
select sku, status, row=row_number() over (partition by status order by date desc)
from Tbal2
)
update Tbl1 set status = u.status from Tbl1 t, updateCTE u
 where t.sku = u.sku
 and u.row = 1
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34241078
update with join: syntax explained here:
http://www.experts-exchange.com/A_1517.html
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
hyphenpipe earned 500 total points
ID: 34241171
Slight modification:

with updateCTE
as
(
select sku, status, row=row_number() over (partition by sku order by date desc)
from Tbl2
)
update Tbl1 set status = u.status from Tbl1 t, updateCTE u
 where t.sku = u.sku
 and u.row = 1
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34241189
Update Tbl1
Set Col3 = (
      select top(1) Tbl2.Col3
      Where Tbl1.Col1 = Tbl2.Col1
      Order by Tbl2.Col4 desc)
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34241457
An expensive way to write top 1
May even cause a table scan to process the row numbering...
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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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