Jump to content

Update rows that are duplicated based on more than one column


Recommended Posts

Hey everyone,

 

Have tried to look around for hours now for a solution to this, but have not found anything suitable yet!

 

Basically, I just want to modify a column in rows are duplicated.

 

Example table:

 

[pre]ID | ProductName | CatID | Hits

-------------------------------

1  | Prod 1      | 1    | 23

2  | Prod 1      | 1    | 34

3  | Prod 1      | 2    | 45

4  | Prod 2      | 3    | 56

5  | Prod 2      | 3    | 67

6  | Prod 3      | 4    | 78

-------------------------------[/pre]

 

The rule is, a row is duplicated when the same ProductName and CatID combination exist on another row, regardless of the ID or Hits values. I would like to get the following results:

 

[pre]ID | ProductName | CatID | Hits

-------------------------------

1  | Prod 1      | 1    | 23

2  | Prod 1      | 1    | 34

4  | Prod 2      | 3    | 56

5  | Prod 2      | 3    | 67

-------------------------------[/pre]

 

Note that I am not interested in how many times it has been duplicated, just the fact it is a duplicate. The key thing is, I need to get at the ID value.

 

Then, I would like to set Hits to 0 on the above, resulting in:

 

[pre]ID | ProductName | CatID | Hits

-------------------------------

1  | Prod 1      | 1    | 0

2  | Prod 1      | 1    | 0

3  | Prod 1      | 2    | 45

4  | Prod 2      | 3    | 0

5  | Prod 2      | 3    | 0

6  | Prod 3      | 4    | 78

-------------------------------[/pre]

 

Ideally I want to do this all in one query. So far I have this query:

 

SELECT ID, ProductName, CatID, Hits
FROM some_table
GROUP BY ProductName, CatID
HAVING COUNT(*) > 1

 

This will only return:

 

[pre]ID | ProductName | CatID | Hits

-------------------------------

1  | Prod 1      | 1    | 23

4  | Prod 2      | 3    | 56

-------------------------------[/pre]

 

Clearly, this is only showing the first duplicate for each!

 

I must be missing something really obvious and I'd really appreciate some help with this one, before I go insane!

 

Thank you very much!

 

Matthew

I'll skip the obvious question about duplicates....

 

You can probably "cheat" in this case... you can use the query you showed to get the ProductName/CatID tuple that have duplicates; but ask for MAX(hits) back.  Then use this as a derived table, and join to the entire table -- and simply issue an update statement where the prod/cat match but the hits don't.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.