0

Can you please tell me what SQL query can I use to change duplicates in one column of my table? I found these duplicates:

SELECT Model, count(*) FROM Devices GROUP BY model HAVING count(*) > 1;

I was looking for information on exactly how to change one of the duplicate values, but unfortunately I did not find a specific option for myself, and all the more information is all in abundance filled by deleting the duplicate value line, which I don't need. Not strong in SQL at all. I ask for help. Thank you so much.

3
  • 2
    Change what value, to what? Commented Jan 27, 2021 at 9:11
  • 1
    @jarlh, I need to replace the found duplicate with the line -. but only with this condition, for example, four models are found in the Model column: Esonic G31CEL2 Esonic G31CEL2 Esonic G31CEL2 Esonic G31CEL2 at the output you need to get: Esonic G31CEL2 - - - Commented Jan 27, 2021 at 9:16
  • 1
    Post sample data and expected results to clarify what you want. Commented Jan 27, 2021 at 10:10

3 Answers 3

1

You can easily use a Window Functions such as ROW_NUMBER() with partitioning option in order to group by Model column to eliminate the duplicates, and then pick the first rows(rn=1) returning from the subquery such as

WITH d AS
(
 SELECT *, ROW_NUMBER() OVER (PARTITION BY Model) AS rn
   FROM Devices 
)
SELECT ID, Model -- , and the other columns
  FROM d
 WHERE rn = 1 

Demo

Sign up to request clarification or add additional context in comments.

Comments

0

use exists as follows:

update d 
   set Model = '-'
  from Devices d
where exists (select 1 from device dd where dd.model = d.model and dd.id > d.id)

7 Comments

Your query contains error. Please check it . :)
I am not able to see any error. Please point out if you find any @Linker.
Alias name.update Devices d creating an error(SQL SERVER 2016). Please check . :)
@Linker Yes, changed it now. Thanks for pointing it out. :)
@Popeye, I am trying to use your request, but I have an error: Execution finished with errors. Result: near ".": syntax error At line 1: update d set d.
|
0

After the command:

SELECT Model, count (*) FROM Devices GROUP BY model HAVING count (*)> 1;

i get the result:

  • 1895 lines = NULL;
  • 3383 lines with duplicate values;
  • and all these values are 1243.

after applying your command:

update Devices set
  Model = '-'
where id not in
  (select
     min(Devices .id)
   from Devices 
   group by Devices.Model)

i got 4035 lines changed. if you count, it turns out, (3383 + 1895) = 5278 - 1243 = 4035 and it seems like everything fits together, the result suits, it works.

2 Comments

It will create another duplicate contained row if the any group does have more than 2 items.
@Linker, it's strange why you think so, for a start I looked, what are the duplicates, there are many of them. I'll update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.