I have a Categories table which has some duplicate Categories as described below,
`Categories`
+========+============+============+
| cat_id | cat_name | item_count |
+========+============+============+
| 1 | Category 1 | 2 |
| 2 | Category 1 | 1 |
| 3 | Category 2 | 2 |
| 4 | Category 3 | 1 |
| 5 | Category 3 | 1 |
+--------+------------+------------+
Here is another junction table which relates to another Items table. The item_count
in the first table is the total number of items per cat_id
.
`Junction`
+========+=========+
| cat_id | item_id |
+========+=========+
| 1 | 100 |
| 1 | 101 |
| 2 | 102 |
| 3 | 103 |
| 3 | 104 |
| 4 | 105 |
| 5 | 106 |
+--------+---------+
How do I add or combine those items from the duplicate Categories into ones each having maximum item_count
among their duplicates? (e.g. Category 1
).
Also, if the item_count
is the same for those duplicate ones, then the Category with maximum cat_id
will be chosen and item_count
will be combined to that record. (e.g. Category 3
).
Note: Instead of removing the duplicate records, the
item_count
will be set to0
.
Below is the expected result.
+========+============+============+
| cat_id | cat_name | item_count |
+========+============+============+
| 1 | Category 1 | 3 |
| 2 | Category 1 | 0 |
| 3 | Category 2 | 2 |
| 4 | Category 3 | 0 |
| 5 | Category 3 | 2 |
+--------+------------+------------+
+========+=========+
| cat_id | item_id |
+========+=========+
| 1 | 100 |
| 1 | 101 |
| 1 | 102 |
| 3 | 103 |
| 3 | 104 |
| 5 | 105 |
| 5 | 106 |
+--------+---------+
In the result, there are two duplicates Category 1
and Category 3
. And we have 2 scenarios,
cat_id
=2
is eliminated because itsitem_count
=1
is less than that ofcat_id
=1
which isitem_count
=2
.cat_id
=4
is eliminated even though itsitem_count
is the same as that ofcat_id
=5
since5
is the maximum among duplicateCategory 3
.
Please help me if any query that can join and update both tables in order to solve the duplicates.