SQL UPDATE using JOIN that matches two or more val

2020-05-05 18:07发布

问题:

I'm performing an SQL UPDATE with JOIN, but that JOIN can match more than one value. Let's say we have the following tables:

    Table_1              Table_2
 col_a | col_b        col_a | col_b
---------------      ---------------
   1   |   A           1    |    X    
   2   |   B           1    |    Y   
   3   |   C           3    |    Z   

And I execute the following query:

UPDATE
  t1
SET
  t1.col_b = t2.col_b
FROM
  Table_1 t1
  JOIN
  Table_2 t2 ON t1.col_a = t2.col_a;

The result is as follows:

    Table_1              Table_2
 col_a | col_b        col_a | col_b
---------------      ---------------
   1   |   X           1    |    X    
   2   |   B           1    |    Y   
   3   |   Z           3    |    Z 

What I need to do is to update the Table_1 with the last value matched; so in this case, I would need this result:

    Table_1              Table_2
 col_a | col_b        col_a | col_b
---------------      ---------------
   1   |   Y           1    |    X    
   2   |   B           1    |    Y   
   3   |   Z           3    |    Z 

回答1:

Provided you have a way to define the order of records in Table_2 (what does last mean?) you can use window functions to filter Table_2 to only include the last record of each group of records that match:

UPDATE
  t1
SET
  t1.col_b = t2.col_b
FROM
  Table_1 t1
  JOIN
  (SELECT col_a, col_b,
          ROW_NUMBER() OVER (PARTITION BY col_a 
                             ORDER BY <order by field list goes here> DESC) AS RNo
   FROM Table_2) t2 ON t1.col_a = t2.col_a AND t2.RNo=1;

In the special case that the order by field is col_b then you can simply use (this works on all versions of SQL Server):

UPDATE
  t1
SET
  t1.col_b = t2.col_b
FROM
  Table_1 t1
  JOIN
  (SELECT col_a, MAX(col_b) AS col_b
   FROM Table_2
   GROUP BY col_a) t2 ON t1.col_a = t2.col_a;


回答2:

Assuming "last" value is the largest value, you could aggregate via cte.

use test
go

/*
create table Table_1 (col_a int, col_b nchar(1));
create table Table_2 (col_a int, col_b nchar(1));

insert Table_1 values
(1,N'A'),
(2,N'B'),
(3,N'C');

insert Table_2 values
(1,N'X'),
(1,N'Y'),
(3,N'Z');
*/

with cte_maxT2 as
(
    select col_a, max(col_b) col_b
    from Table_2
    group by col_a
)
select t1.*,c.*
from Table_1 t1
join cte_maxT2 c on c.col_a=t1.col_a