[英]How to update distinct column in SQL Server 2005?
In my table I have a column Segment_No
which has duplicates. 在我的表格中,我有一列
Segment_No
,其中有重复项。 Now I want to update those duplicates. 现在,我想更新那些重复项。
For example: the value 249X5601 is present in two rows. 例如: 249X5601值出现在两行中。 I want to change the second value to 249X5601R
我想将第二个值更改为249X5601R
The general form would be as follows: 一般形式如下:
;with AllRows as (
select Donation_ID,Registration_No,Segment_No,
ROW_NUMBER() OVER (
PARTITION BY Segment_No
order by <Suitable_Column>
) as rn
from UnnamedTable
)
update AllRows set Segment_no = <New_Value>
where rn > 1
Where <Suitable_Column>
gives the column(s) the define which row is "first" and which is second. 在
<Suitable_Column>
给出列的地方,定义哪一行是“第一”,哪一行是第二。 <New_Value>
defines how the new Segment_no
value should be computed, and rn
gives the row numbers - so the where clause is ignoring the "first" row. <New_Value>
定义应如何计算新的Segment_no
值,并且rn
给Segment_no
号-因此where子句将忽略“第一”行。
So if there are only ever a max of two rows sharing a single Segment_no
value, and the "first" is the one with the lowest Donation_ID
value, then it would be: 因此,如果最多只有两行共享一个
Segment_no
值,并且“第一个”是Donation_ID
值最低的行,那么它将是:
;with AllRows as (
select Donation_ID,Registration_No,Segment_No,
ROW_NUMBER() OVER (
PARTITION BY Segment_No
order by Donation_ID
) as rn
from UnnamedTable
)
update AllRows set Segment_no = Segment_no + 'R'
where rn > 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.