根据table1,将VALUE值同步到table2,要求取table1中最大月份对应的VALUE。例如id为1最大月份为201102,则对应到table2的VALUE值为112.table1:
id ym value
1 201101 100
2 201101 231
3 201101 231
1 201102 112
2 201102 334
3 201102 554
7 201101 111
8 201101 223
9 201101 112
7 201102 334
8 201102 556
9 201102 545
7 201103 444
8 201103 555
9 201103 655
10 201103 434
table2:
id value
1
2
3
4
5
6
7
8
9
10
id ym value
1 201101 100
2 201101 231
3 201101 231
1 201102 112
2 201102 334
3 201102 554
7 201101 111
8 201101 223
9 201101 112
7 201102 334
8 201102 556
9 201102 545
7 201103 444
8 201103 555
9 201103 655
10 201103 434
table2:
id value
1
2
3
4
5
6
7
8
9
10
update a
set a.value=b.value
from table2 a
inner join
(select id,value,
row_number() over(partition by id order by ym desc) 'rn'
from table1) b on a.id=b.id
where b.rn=1
create table table1(id int,ym int,value int)
insert into table1
select 1,201101,100
union all select 2,201101,231
union all select 3,201101,231
union all select 1,201102,112
union all select 2,201102,334
union all select 3,201102,554
union all select 7,201101,111
union all select 8,201101,223
union all select 9,201101,112
union all select 7,201102,334
union all select 8,201102,556
union all select 9,201102,545
union all select 7,201103,444
union all select 8,201103,555
union all select 9,201103,655
union all select 10,201103,434
create table table2(id int,value int)
insert into table2(id)
select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10update A
set value=B.value
from table2 A
inner join (select id,max(value) as value from table1 group by id)B
on a.id=b.idselect * from table2
drop table table1,table2
/*
id value
----------------
1 112
2 334
3 554
4 NULL
5 NULL
6 NULL
7 444
8 556
9 655
10 434*/
update table2 set a.value=b.value
from table2 a
inner join
(select id,value,
row_number() over(partition by id order by ym desc) num from table1) b on a.id=b.id
where b.num=1
--drop table table2create table table1(id int,ym int,value int)insert into table1
select 1,201101,100
union all select 2,201101,231
union all select 3,201101,231
union all select 1,201102,112
union all select 2,201102,334
union all select 3,201102,554
union all select 7,201101,111
union all select 8,201101,223
union all select 9,201101,112
union all select 7,201102,334
union all select 8,201102,556
union all select 9,201102,545
union all select 7,201103,444
union all select 8,201103,555
union all select 9,201103,655
union all select 10,201103,434
create table table2(id int,value int)
insert into table2(id)
select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10;with t
as
(
select id,ym,value,
row_number() over(partition by id order by ym desc) as rownum
from table1
)update table2
set value = t1.value
from table2 t2
inner join t t1
on t1.id = t2.id
and t1.rownum = 1select * from table2
/*
id value
----------------
1 112
2 334
3 554
4 NULL
5 NULL
6 NULL
7 444
8 556
9 655
10 434*/
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-24 10:19:54
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[table1]
if object_id('[table1]') is not null drop table [table1]
go
create table [table1]([id] int,[ym] int,[value] int)
insert [table1]
select 1,201101,100 union all
select 2,201101,231 union all
select 3,201101,231 union all
select 1,201102,112 union all
select 2,201102,334 union all
select 3,201102,554 union all
select 7,201101,111 union all
select 8,201101,223 union all
select 9,201101,112 union all
select 7,201102,334 union all
select 8,201102,556 union all
select 9,201102,545 union all
select 7,201103,444 union all
select 8,201103,555 union all
select 9,201103,655 union all
select 10,201103,434
--> 测试数据:[table2]
if object_id('[table2]') is not null drop table [table2]
go
create table [table2]([id] int,[value] sql_variant)
insert [table2]
select 1,null union all
select 2,null union all
select 3,null union all
select 4,null union all
select 5,null union all
select 6,null union all
select 7,null union all
select 8,null union all
select 9,null union all
select 10,null
--------------开始查询--------------------------UPDATE table2
SET table2.value=a.value
from [table1] a INNER JOIN table2 ON a.id=table2.id
WHERE EXISTS (SELECT 1 FROM (SELECT id,MAX(ym)ym FROM table1 GROUP BY id)b WHERE a.id=b.id AND a.ym=b.ym)
SELECT * FROM table2
----------------结果----------------------------
/*
id value
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 112
2 334
3 554
4 NULL
5 NULL
6 NULL
7 444
8 555
9 655
10 434
*/
select 1,'201101',100
union all
select 2,'201101',231
union all
select 3,'201101',231
union all
select 1,'201102',112
union all
select 2, '201102',334
union all
select 3, 201102, 554
union all
select 7, 201101, 111
union all
select 8, 201101, 223
union all
select 9, 201101, 112
union all
select 7 ,201102, 334
union all
select 8 ,201102, 556
union all
select 9 ,201102 ,545
union all
select 7 ,201103, 444
union all
select 8, 201103 ,555
union all
select 9, 201103, 655
union all
select 10, 201103, 434
create table tab2(id int,value int)insert into tab2 (id) values(1)
insert into tab2 (id) values(2)
insert into tab2 (id) values(3)
insert into tab2 (id) values(4)
insert into tab2 (id) values(5)
insert into tab2 (id) values(6)
insert into tab2 (id) values(7)
insert into tab2 (id) values(8)
insert into tab2 (id) values(9)
insert into tab2 (id) values(10)
update a set a.value=(select max(value) from tk2 where id=a.id and ym=(select max(ym) from tk2 where id=a.id)) from tab2 aid value
----------- -----------
1 112
2 334
3 554
4 NULL
5 NULL
6 NULL
7 444
8 555
9 655
10 434(所影响的行数为 10 行)