从第二个表替换分组ID

时间:2014-09-01 07:15:17

标签: sql sql-server

我有两张桌子。

Table1
    Id, Column1, Column2, Column3      
     1     1       2        3
     2     1       2        3
     3     2       1        3
     4     2       1        3
     5     2       2        2

Table2        
      Id, Table1_Id         
       1      1
       2      1
       3      2
       4      2
       5      2
       6      3
       7      4
       8      4
       9      5

从第一个表我需要删除具有相同Column1,Column2和Column3值的重复行,并且表2我需要从Table1中删除重复行后,用Id替换FKs Table1_Id列。

我需要这个

Table1   
Id, Column1, Column2, Column3
 1     1       2        3
 3     2       1        3
 5     2       2        2

Table2  
   Id, Table1_Id  
    1      1
    2      1
    3      1
    4      1
    5      1
    6      3
    7      3
    8      3
    9      5

3 个答案:

答案 0 :(得分:1)

-- Build test data

create table #table1(
    id int,
    column1 int,
    column2 int,
    column3 int
)

create table #table2(
    id int,
    table1_id int
)

insert into #table1
select 1, 1, 2, 3 union all
select 2, 1, 2, 3 union all
select 3, 2, 1, 3 union all
select 4, 2, 1, 3 union all
select 5, 2, 2, 2

insert into #table2
select 1, 1 union all
select 2, 1 union all
select 3, 2 union all
select 4, 2 union all
select 5, 2 union all
select 6, 3 union all
select 7, 4 union all
select 8, 4 union all
select 9, 5

-- update rows of table2
;with cte as(
    select
        *,
        rn = row_number() over(partition by column1, column2, column3 order by id)
    from #table1
)
update t2 
    set t2.table1_id = c.id
from #table2 t2 
inner join #table1 t1
    on t1.id = t2.table1_id
inner join cte c on
    c.column1 = t1.column1
    and c.column2 = t1.column2
    and c.column3 = t1.column3
    and c.rn = 1

-- delete duplicate rows of table1
;with cte as(
    select
        *,
        rn = row_number() over(partition by column1, column2, column3 order by id)
    from #table1
)
delete from cte where rn > 1 

select * from #table1
select * from #table2

--drop test data
drop table #table1
drop table #table2

答案 1 :(得分:1)

-- test tables
declare @t1 table
(Id int, Column1 int, Column2 int, Column3 int)
declare @t2 table
(Id int, Column1 int)

-- data
insert @t1 values
(1,1,2,3),(2,1,2,3),(3,2,1,3),(4,2,1,3),(5,2,2,2)

insert @t2 values (1,1)
,(2,1),(3,2),(4,2),(5,2)
,(6,3),(7,4),(8,4),(9,5)

-- update t2
begin transaction
;with cte as(
    select
      id,
      lowrow_id = min(id) over(partition by column1, column2, column3)
    from @t1
)
update t2
set 
  Column1 = cte.lowrow_id
from 
@t2 t2
join cte
on t2.Column1 = cte.id
where cte.lowrow_id < cte.id

-- delete t1
;with cte as(
    select
      id,
      lowrow_id = min(id) over(partition by column1, column2, column3)
    from @t1
)
delete cte
where
  lowrow_id < id

commit transaction

-- show result
select * from @t1
select * from @t2

输出

Id  Column1 Column2 Column3
1   1       2       3
3   2       1       3
5   2       2       2

Id  Column1
1   1
2   1
3   1
4   1
5   1
6   3
7   3
8   3
9   5

答案 2 :(得分:0)

with tbl1 as (
select ROW_NUMBER() over (partition by column1,column2,column3 order by id) as rownb,min(id) over (partition by column1,column2,column3) as pid,id,column1,column2,column3 from Table1
)
update table2 set Table1_id = tbl1.pid from table2 left join tbl1 on table2.Table1_id = tbl1.id 

with tbl1 as (
select ROW_NUMBER() over (partition by column1,column2,column3 order by id) as rownb,min(id) over (partition by column1,column2,column3) as pid,id,column1,column2,column3 from Table1
)
delete from tbl1 where rownb > 1