Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL - Update from top 1 in another table

Posted on 2010-11-30
7
Medium Priority
?
380 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 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