Answer the question
In order to leave comments, you need to log in
How to remove duplicates and sum cells?
Hello. There is such a table:
+----+-------------------------+------+------+----------+--------------+---------+-------+----------+--------+------+
| id | date_id | year | week | store_id | store_id_new | good_id | promo | quantity | order | cost |
+----+-------------------------+------+------+----------+--------------+---------+-------+----------+--------+------+
| 1 | 2015-12-07 00:00:00.000 | NULL | NULL | 5 | NULL | 26 | 0 | 2 | 764536 | NULL |
+----+-------------------------+------+------+----------+--------------+---------+-------+----------+--------+------+
| 2 | 2015-12-07 00:00:00.000 | NULL | NULL | 5 | NULL | 26 | 0 | 1 | 764536 | NULL |
+----+-------------------------+------+------+----------+--------------+---------+-------+----------+--------+------+
+----+-------------------------+------+------+----------+--------------+---------+-------+----------+--------+------+
| id | date_id | year | week | store_id | store_id_new | good_id | promo | quantity | order | cost |
+----+-------------------------+------+------+----------+--------------+---------+-------+----------+--------+------+
| 1 | 2015-12-07 00:00:00.000 | NULL | NULL | 5 | NULL | 26 | 0 | 3 | 764536 | NULL |
+----+-------------------------+------+------+----------+--------------+---------+-------+----------+--------+------+
Answer the question
In order to leave comments, you need to log in
So like
select id , date_id, year , week , store_id , store_id_new , good_id , promo , SUM(quantity) , order , cost
from table
group by
id , date_id, year , week , store_id , store_id_new , good_id , promo , order , cost
That is, in the GROUP BY field we add all the fields for which arithmetic operations are not needed. In this case, I saw only quantity
If this is really necessary, then the easiest way is to insert a selection into a temporary table within the transaction, delete "duplicates" from the main one and insert the aggregation result from the temporary into the main one. Here the question really arises id (I hope it is pk?)
Criteria for removing duplicates: leave with a minimum id
?
You make a cursor with grouping by date_id, store_id, good_id и order
and a filter: the number of lines is more than one. For each row of the cursor, determine the minimum id
and update quantity
the sum over date_id, store_id, good_id и order
. Delete the extra ones. Move to the next line of the cursor.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question