Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate duplicate entries in sql

Tags:

sql

I want to truncate duplicate rows but Qty should be added.

I have a table filled with data,

Item Qty MinQty MaxQty
ABC  10  20     50
XYZ  12  30     40
ABC  15  20     50

I want the result like,

Item Qty MinQty MaxQty
ABC  25  20     50
XYZ  12  30     40

Kindly help me to write the query for the same...

like image 755
user2190327 Avatar asked Dec 16 '22 08:12

user2190327


2 Answers

SELECT Item, SUM(Qty), MIN(MinQty), MAX(MaxQty)
FROM tablename
GROUP BY ITem;
like image 145
Mahmoud Gamal Avatar answered Jan 11 '23 12:01

Mahmoud Gamal


The answer above is right, but you would also want to give the derived columns names:

SELECT Item, SUM(Qty) as Qty, MIN(MinQty) as MinQty, MAX(MaxQty) as MaxQty
FROM tablename
GROUP BY ITem;
like image 28
Metaphor Avatar answered Jan 11 '23 10:01

Metaphor