Link to home
Start Free TrialLog in
Avatar of Star79
Star79Flag for United States of America

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.
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Star79

ASKER

Thank you it helped!
Wouldn't a simple sum and group by work?
select productid, locatorid, attributeid, sum(qtyonhand) as qtyonhand
from yourtable
group by productid, locatorid, attributeid;
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>> 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.