Skip to content
Advertisement

Update just one of the row that have the same ID SQL Server Query

I have two table. Table1 and Table2 , i want to update Table 2 with Table 1 field, and i want to update just one of the row with same CN number in table2

Table1
CN      SLT
1       1.2
2       19
3       23

Table2
CN      SLT
1       0
1       0
1       0
2       0
2       0
3       0

After Update query

Table2
CN      SLT
1       1.2
1       0
1       0
2       19
2       0
3       23

Advertisement

Answer

Use a join with row_number() to get just one row per id:

update t1
    set t1.slt = t2.slt
from (select t1.*, row_Number() over (partition by id order by id) as seqnum
      from table1 t1
     ) t1 join
     table2 t2
     on t1.cn = t2.cn
where t1.seqnum = 1;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement