简体   繁体   English

更新/合并具有求和值的表

[英]Update / Merge table with summing values

I have a schema: 我有一个架构:

http://sqlfiddle.com/#!4/e9917/1 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. 我想通过添加具有相同(id,期间)对的记录数量来更新数量字段。 like after op : 就像在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: 在实际情况下,此表是通过其他2个表的插入操作填充的:

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 新的sqlfiddle链接: http ://sqlfiddle.com/#!4/cd45b/1

May be it can be solved during insertion from two table.. 也许可以在从两个表插入时解决。

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. 但是,您实际上应该决定test_table是否具有主键以及总量或所有明细数据。 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: 根据您所添加的内容,那么我可以说您可以使用Oracle MERGE INTO语句:

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. 请注意...仅当test_table已经没有重复的ID(以句点行开头)时,此方法才起作用。 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). 因此,如果您的表已经搞砸了,那么您仍然必须在第一次时正确地对其进行初始化(并可能添加唯一的ID,句点键,以避免将来出现问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM