【问题标题】:Oracle SQL : Updating a column with SUM query of another tableOracle SQL:使用另一个表的 SUM 查询更新列
【发布时间】:2015-01-29 13:27:25
【问题描述】:

我有两张桌子: Table1 有 5 列(col1、col2、col3、col4、val1)和 Table2 有 Table1 有 5 列(col1、col2、col3、col4、val2)。 1. Table1.val1 不包含任何值。 2、对于Table1和Table2,col1、col2、col3、col4都是一样的,都是主键。

我想做的是在 Table1.col1 = Table2.col1 和 Table1.col2 = Table2.col2 和 Table1.col3 = Table2.col3 时用 sum(Table2.val2) 更新 Table1.val1 和表 1.col4 = 表 2.col4。

我做了类似的事情:

UPDATE Table1
SET val1 = (

select t_sommevbrute.sumvalbrute from (
Select  col1,col2,col3,col4,SUM(val2) sumvalbrute
From Table2
Where col3 = 2014 And col2=51 
GROUP BY col1, col2, col3, col4) t_sommevbrute

WHERE Table1.col1 = t_sommevbrute.col1
and Table1.col2 = t_sommevbrute.col2
and Table1.col3 = t_sommevbrute.col3
and Table1.col4 = t_sommevbrute.col4)

但与这个问题有关:Oracle SQL: Update a table with data from another table,我应该有WHERE EXISTS 子句。

请帮忙!! 谢谢。

【问题讨论】:

  • 您的子查询不相关。

标签: sql oracle select


【解决方案1】:

您可以通过以下方式执行此操作:-

使用临时表:- 首先创建临时表:-

Create table temp1 as
Select  col1,col2,col3,col4,SUM(val2) as sumvalbrute
     From table2
     Where col3 = 3 And col2=2 
     GROUP BY col1, col2, col3, col4;

然后使用Temp表更新Main Table1:-

UPDATE table1 SET table1.val1 = (SELECT temp1.sumvalbrute
                                  FROM temp1 
                                  WHERE Table1.col1 = temp1.Col1
                                  AND Table1.col2 = temp1.Col2
                                  AND Table1.col3 = temp1.Col3
                                  AND Table1.col4 = temp1.Col4);

SQL 小提琴:- http://sqlfiddle.com/#!4/4864d/5

不使用临时表:-

UPDATE table1 SET table1.val1 = (SELECT temp1.sumvalbrute
                                 FROM 
                                 (Select  col1,col2,col3,col4,SUM(val2) as sumvalbrute
                                  From table2
                                  Where col3 = 3 And col2=2 
                                  GROUP BY col1, col2, col3, col4) temp1 
                                  WHERE Table1.col1 = temp1.Col1
                                  AND Table1.col2 = temp1.Col2
                                  AND Table1.col3 = temp1.Col3
                                  AND Table1.col4 = temp1.Col4);

SQL 小提琴:- http://sqlfiddle.com/#!4/c9286/2

【讨论】:

  • 是的,但我不想创建临时表
  • 那试试直接用Select Query used temp table, in Update Query?
  • 我无权创建表,然后我必须找到创建任何其他表的解决方案
  • 好的,我将在不使用临时表的情况下更新答案。
  • 这正是我所做的!
【解决方案2】:

您可以大大简化您的查询:

UPDATE Table1
    SET val1 = (select SUM(val2)
                from Table2 t
                where Table1.col1 = t.col1 and
                      Table1.col2 = t.col2 and
                      Table1.col3 = t.col3 and
                      Table1.col4 = t.col4
               )
    Where col3 = 2014 And col2 = 51;

可以使用where exists。目的是防止NULL 值不匹配时。但是,我认为将where 子句拉到外面可能会解决这个问题。我注意到where 子句使用与相关条件相同的值。

【讨论】:

  • 如您所见,我有一个 group by 子句。由于表2中的行可能重复,所以我有分组和总和。你不这么认为吗?
  • @MDIT 。 . .不。带有相关条件的sum() 应该做你想做的事。
  • 但是 where 子句“Where col3 = 2014 And col2 = 51”涉及 table2
  • @MDIT 。 . .由于相关条件,这在任一表上都是相同的。
  • 因为我仍然需要 Table2 中的列名,而我没有在上面的表结构中显示
【解决方案3】:

你可以试试这个:

UPDATE t1 SET t1.val1 = SUM(t2.val2)
FROM table1
INNER JOIN table2 t2 ON t2.col1 = t1.col1 
and t2.col2 = t1.col2 
and t2.col3 = t1.col3 
and t2.col4 = t2.col4

【讨论】:

    【解决方案4】:

    这种更新也可以使用MERGE语句来实现:

    Merge into Table1 using ( select SUM(val2) as val2, t.col1, t.col2, t.col3, t.col4
                                from Table2 t
                               group by t.col1, t.col2, t.col3, t.col4
                             ) t -- This is the alias for table 2
       on (    Table1.col1 = t.col1 
           and Table1.col2 = t.col2 
           and Table1.col3 = t.col3 
           and Table1.col4 = t.col4
          )
    when matched then 
      UPDATE Table1.val1 = t.val2 --HERE IS YOUR **UPDATE**
    where Table1.col3 = 2004
      and Table1.col2 = 51
     ;
    
    commit;
    

    请不要忘记将别名添加到表中,并在此代码之后进行良好的 COMMIT 该解决方案避免您创建临时表,并且效果很好

    最好的问候。

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多