Answer the question
In order to leave comments, you need to log in
How to make Update taking a value from a table sorted in reverse order?
Let's say there is a query like this:
update Table1
set Table1.F1= Table2.F2
from Table2
where Table1.F3= Table2.F4
The problem is that for each record, set occurs at the first match of F3 and F4, but I need to find the last match . I would write like this (but alas, such a query is not valid):
update Table1
set Table1.F1= Table2.F2
from (select * from Table2 order by Id desc) Table2
where Table1.F3= Table2.F4
How to write a query correctly?
Answer the question
In order to leave comments, you need to log in
Something like this
update Table1
set Table1.F1= Table2.F2
where Table1.F3=(select top 1 F4 from Table2 order by Id desc )
update Table1
set F1 = (SELECT TOP 1 (F2) FROM Table2 T2 WHERE T1.F3 = T2.F4 order by Id desc)
from Table1 t1
update Table1
set F1 = (SELECT MAX(F2) FROM Table2 T2 WHERE T1.F3 = T2.F4 GROUP BY F2)
from Table1 t1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question