Star79
asked on
Add up Duplicate rows in Oracle 10g
Add up Duplicate rows in Oracle 10g
Iam trying to add up duplicate rows in oracle
I have a table with data
PRODUCTID LOCATORID ATTRIBUTEID QTYONHAND
1 2 3 -12
1 2 3 -6
Iam trying to make this into a single row as
PRODUCTID LOCATORID ATTRIBUTEID QTYONHAND
1 2 3 -18
1 2 3 0
Basically zero out the duplicate row and add up the qty in the other row.
Is this feasible,Please let me know the approach.
Iam trying to add up duplicate rows in oracle
I have a table with data
PRODUCTID LOCATORID ATTRIBUTEID QTYONHAND
1 2 3 -12
1 2 3 -6
Iam trying to make this into a single row as
PRODUCTID LOCATORID ATTRIBUTEID QTYONHAND
1 2 3 -18
1 2 3 0
Basically zero out the duplicate row and add up the qty in the other row.
Is this feasible,Please let me know the approach.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Wouldn't a simple sum and group by work?
select productid, locatorid, attributeid, sum(qtyonhand) as qtyonhand
from yourtable
group by productid, locatorid, attributeid;
select productid, locatorid, attributeid, sum(qtyonhand) as qtyonhand
from yourtable
group by productid, locatorid, attributeid;
>> Wouldn't a simple sum and group by work?
No. You wouldn't get the 0 for the "duplicates". The simple sum would return one row not the two rows in the expected results.
DOH! Got it, didn't even notice the row with the zero quantity.
ASKER