Solved

SQL - Update from top 1 in another table

Posted on 2010-11-30
7
352 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 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