Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL - Update from top 1 in another table

Posted on 2010-11-30
7
Medium Priority
?
384 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
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

972 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