简体   繁体   English

如何在SQL Server 2005中更新非重复列?

[英]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值,并且rnSegment_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM