I have got a table that got a column with duplicate values. I would like to update one of a 2 duplicate values so for example row1 = tom
and row2 = tom
.. I want to add a 1 or an a to one of them and that will be for many other duplicates in the same column. Basically just add one number or letter to every 1 of the duplicates so there's no more duplicates.
I got this query that will update all the duplicates but not one of them. Can anyone help?
UPDATE Table1
SET Column1 = 'a'
WHERE exists
(SELECT Column1 , COUNT(Column1 )
FROM Clients
GROUP BY Column1
HAVING ( COUNT(Column1 ) > 1)
)
Try This with CTE
and PARTITION BY
;WITH cte AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY Column1 ORDER BY Column1 ) AS rno,
Column1
FROM Clients
)
UPDATE cte SET Column1 =Column1 +' 1 '
WHERE rno=2
I think this simple update is what you're looking for;
UPDATE Table1 SET Column1=Column1+CAST(id AS VARCHAR)
WHERE id NOT IN (
SELECT MIN(id)
FROM Table1
GROUP BY Column1
);
Input:
(1,'A'),
(2,'B'),
(3,'A'),
(4,'C'),
(5,'C'),
(6,'A');
Output:
(1,'A'),
(2,'B'),
(3,'A3'),
(4,'C'),
(5,'C5'),
(6,'A6');
我认为您可以使用 TOP() 运算符而不是 row_number() 方法,它将帮助您以简单的方式更新一个
UPDATE TOP ( 1 )Table1 SET Column1 = 'a';
Assume Table1
, containing the following information:
Column1
========
tom
john
jack
tom
james
jane
Notice that the first and fourth rows are identical. Here's the UPDATE
command to change the name in only one of them.
UPDATE Table1 AS t1
SET Column1 = 'jennifer'
WHERE rrn(t1) =
(SELECT MAX(rrn(t2))
FROM Table1 AS t2
WHERE Column1 = 'tom')
And the result would be
Column1
========
tom
john
jack
jennifer
james
jane
with RRN
function you can update the last occurance of duplicate record
If we have this condition:
SELECT Column1 FROM Clients ORDER BY Column1
Column1
**********
Jhon
Jhon
Mike
Mike
Mike
Robert
This will work even if Column1
was duplicated n times, it will append the row number to every duplicate row except the first, try this:
BEGIN
;WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY Column1) AS ROWNUMBER,
Column1
FROM Clients
)
UPDATE CTE
SET Column1 = CONCAT(Column1, ' ', (ROWNUMBER - 1))
WHERE ROWNUMBER > 1
SELECT Column1 FROM Clients ORDER BY Column1
END
Result:
Column1
***********
Jhon
Jhon 1
Mike
Mike 1
Mike 2
Robert
try this
with test as
(
select ROW_NUMBER() over (order by salary)rr, salary , emp_no
from salary
)update test set emp_no=10007 where emp_no='10002' and rr=3
Try this mate
UPDATE Table1
SET Column1 = column1 +'a'
WHERE exists(
select row
from (
SELECT
Column1 ,
Row_Number() over(Partition by Column1 order by Column1) as row
FROM Clients
) as subquery
where subquery.row = 2
)
I had to put the with inside a from since my sql developer didnt like updating a with Copying from the answer from Nithesh
it ended up looking like this:
UPDATE Clients
SET Column1 = 'a'
WHERE ColumnID IN (select ColumnID from
(
SELECT
row_number() OVER(PARTITION BY Column1 ORDER BY Column1 ) AS rno,
ColumnID FROM Clients
)
where rno=2
)
Using sub-queries might work:
UPDATE table1
SET STATUS = 'F'
WHERE column1=
(SELECT (column1)
FROM table1
GROUP BY column1
HAVING (COUNT(column1 ) > 1))
update wmwhse1.sku
set descr= concat (descr,'.')
where exists
(select SKU,count (DESCR)
from wmwhse1.sku
group by descr
having (count (DESCR)>1))
all sku description is updating when I run this script.
output: only 1 row should be affected
I have found this solution:
My table devices already have data and the "serial" column should be unique. The "id" is a primary key. A random 6 digits value is concatenated after the original value.
UPDATE devices
SET serial=CONCAT(serial,'_',LPAD(FLOOR(RAND() * 999999.99), 6, '0'))
where id in
(select * FROM(
SELECT d1.id as id
FROM devices as d1, devices as d2
WHERE d1.id <> d2.id and d1.serial=d2.serial) as subdevices
)
Disclaimer:
Question: UPDATE last Id with 'D' where year, month is duplicate memberwise.
-- Sample Table Creation
DECLARE @TempTable TABLE
( Id INT IDENTITY (1, 1) NOT NULL, MemberId INT, for_year INT, for_month INT, for_status CHAR(1) )
-- Sample Insert
INSERT INTO @TempTable
SELECT '2', '2077', '11', 'A' UNION ALL
SELECT '2', '2077', '12', 'A' UNION ALL
SELECT '2', '2077', '12', 'A' UNION ALL
SELECT '3', '2077', '11', 'A' UNION ALL
SELECT '3', '2077', '12', 'A' UNION ALL
SELECT '3', '2077', '12', 'A' UNION ALL
SELECT '77', '2076', '10', 'A' UNION ALL
SELECT '77', '2076', '11', 'A' UNION ALL
SELECT '77', '2076', '12', 'A'
-- where expectation meets reality :) -- logic / main query
UPDATE @TempTable SET for_status = 'D' WHERE Id IN
(
SELECT MAX(Id) FROM @TempTable GROUP BY MemberId, for_year, for_month HAVING COUNT(*)>1
)
-- Sample Select
SELECT * FROM @TempTable
I have found one solution:
Insert a dummy column in the table
Update the values in column and give random value
Update the row using that dummy column's value and drop the column.
Step 1 :
Alter table table_name
ADD column dummy_column
Step 2:
Update table_name
SET dummy_column = (RAND() * 10)
where duplicate_column = duplicate_value
Step 3:
Update table_name
SET duplicate_column = new_value
where dummy_column = random_value
Step 4:
Alter table table_name
drop column dummy_column
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.