Question

I have a schema:

http://sqlfiddle.com/#!4/e9917/1

CREATE TABLE test_table (
  id NUMBER,
  period NUMBER,
  amount NUMBER
);

INSERT INTO test_table VALUES (1000, 1, 100);
INSERT INTO test_table VALUES (1000, 1, 500);
INSERT INTO test_table VALUES (1001, 1, 200);
INSERT INTO test_table VALUES (1001, 2, 300);
INSERT INTO test_table VALUES (1002, 1, 900);
INSERT INTO test_table VALUES (1002, 1, 250);

I want to update the amount field by adding amounts of records which has same (id, period) pair. like after op :

  ID|  period|   amount
1000         1      600
1001         1      200
1001         2      300
1002         1     1150 

I Couldn't figure out how :(

EDIT:

In actual case this table is populated by insertion operation from other 2 tables:

CREATE TABLE some_table1(
  id NUMBER,
  period NUMBER,
  amount NUMBER
);

INSERT INTO some_table1 VALUES (1000, 1, 100);
INSERT INTO some_table1 VALUES (1000, 1, 500);
INSERT INTO some_table1 VALUES (1001, 1, 200);
INSERT INTO some_table1 VALUES (1001, 2, 300);
INSERT INTO some_table1 VALUES (1002, 1, 900);
INSERT INTO some_table1 VALUES (1002, 1, 250);

CREATE TABLE some_table2(
  id NUMBER,
  period NUMBER,
  amount NUMBER
);

INSERT INTO some_table2 VALUES (1000, 1, 30);
INSERT INTO some_table2 VALUES (1000, 1, 20);
INSERT INTO some_table2 VALUES (1001, 1, 15);
INSERT INTO some_table2 VALUES (1001, 2, 20);
INSERT INTO some_table2 VALUES (1002, 1, 50);
INSERT INTO some_table2 VALUES (1002, 1, 60);

Dublicates occures when two insertions done:

INSERT INTO TEST_TABLE (id,period,amount) SELECT id,period,amount from some_table1
INSERT INTO TEST_TABLE (id,period,amount) SELECT id,period,amount from some_table2

new sqlfiddle link: http://sqlfiddle.com/#!4/cd45b/1

May be it can be solved during insertion from two table..

Was it helpful?

Solution

A script like this would do what you want:

CREATE TABLE test_table_summary (
  id NUMBER,
  period NUMBER,
  amount NUMBER
);

INSERT INTO test_table_summary (id, period, amount)
SELECT id, period, SUM(amount) AS total_amount FROM test_table
GROUP BY id, period;

DELETE FROM test_table;

INSERT INTO test_table (id, period, amount)
SELECT id, period, total_amount FROM test_table_summary;

DROP TABLE test_table_summary;

But you should actually decide if test_table is to have a primary key and the total amount or all the detail data. It's not a good solution to use one table for both.

By what you have added, then I'd say you can use the Oracle MERGE INTO statement:

MERGE INTO test_table t
   USING (SELECT id, period, amount FROM some_table1) s
   ON (t.id=s.id AND t.period=s.period)
   WHEN MATCHED THEN UPDATE SET t.amount=t.amount+s.amount
   WHEN NOT MATCHED THEN INSERT (t.id, t.period, t.amount)
     VALUES (s.id, s.period, s.amount);

Beware though... this will work only if test_table already has no duplicate id, period rows to begin with. So if your table is already messed up, you still have to reinitialize it properly a first time (and maybe add a unique id, period key to avoid problems in the future).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top