Sql 使用来自不同行源的相同键更新行

Sql 使用来自不同行源的相同键更新行,sql,sap-iq,Sql,Sap Iq,我有下表表1: numseq Article ID Num1 Num2 Num3 Num4 Col07 Col08 3023 F 242840794 4 12 1 13 RCN03 9 3023 F 242840794 4 12 1 13

我有下表表1:

numseq  Article     ID          Num1        Num2    Num3   Num4    Col07    Col08
3023    F           242840794       4           12        1    13          RCN03    9   
3023    F           242840794       4           12        1    13          RCN03    9   
3023    F           242840794       4           12        1    13          RCN03    9   
Col07的值为false,有一个5字符而不是6字符

现在,我在表2中得到了更正后的数据

numseq  Article     ID              Num1        Num2    Num3   Num4    Col07     Col08
3023    F           242840794       4           12        1    13      RCN031    9   
3023    F           242840794       4           12        1    13      RCN035    9   
3023    F           242840794       4           12        1    13      RCN037    9   
更新Table1.Col07的键是所有其他列

如何进行更新

我使用Sybase IQ 15.1


我尝试使用row_number(),但该函数不受支持。

Sybase支持
更新中的
加入

update t1
    set col7 = t2.col7
    from table1 t1 join
         table2 t2
         on t1.numseq = t2.numseq and
            t1.article = t2.article and
            . . . ;  -- I'm not sure which columns are needed for matching

Sybase支持
更新中的
加入

update t1
    set col7 = t2.col7
    from table1 t1 join
         table2 t2
         on t1.numseq = t2.numseq and
            t1.article = t2.article and
            . . . ;  -- I'm not sure which columns are needed for matching

您尚未说明如何将表1中的哪一行与表2中的哪一行进行匹配

建议的联接列在两个表中具有相同的值;最终的结果是,在建议的联接列上联接这两个表将产生9行笛卡尔积(表2中的每一行与表1中的每一行匹配)


我猜答案大概是。。。对于表2中的每一行,连接到表1中的任何一行。。。本问答的全部目的是如何将每个联接限制为表1中的一行

虽然可能有一些巧妙的方法可以做到这一点(注意:我不使用IQ),但我认为以下内容可能更容易实现,也更容易理解:

begin tran

delete Table1 where ... >>conditions that match the 3 duplicate rows<< ...

insert Table1 select * from Table2 where ... >>conditions that match the 3 new rows<< ...

if no errors
    commit tran
else
    rollback tran
    generate additional error message ?
开始传输

删除表1,其中…>>匹配3个重复行的条件>匹配3个新行的条件您没有说明如何匹配表1中的哪一行和表2中的哪一行

建议的联接列在两个表中具有相同的值;最终的结果是,在建议的联接列上联接这两个表将产生9行笛卡尔积(表2中的每一行与表1中的每一行匹配)


我猜答案大概是。。。对于表2中的每一行,连接到表1中的任何一行。。。本问答的全部目的是如何将每个联接限制为表1中的一行

虽然可能有一些巧妙的方法可以做到这一点(注意:我不使用IQ),但我认为以下内容可能更容易实现,也更容易理解:

begin tran

delete Table1 where ... >>conditions that match the 3 duplicate rows<< ...

insert Table1 select * from Table2 where ... >>conditions that match the 3 new rows<< ...

if no errors
    commit tran
else
    rollback tran
    generate additional error message ?
开始传输

删除表1,其中…>>匹配3个重复行的条件>匹配3个新行的条件注意:我没有访问IQ数据库的权限;我确实可以访问SQLAnywhere数据库;IQ参考手册中显示了以下使用的功能;建议的解决方案在我的SQLAnywhere数据库中工作;最终结果是,以下内容应该在IQ中起作用…>>交叉手指注意:我没有访问IQ数据库的权限;我确实可以访问SQLAnywhere数据库;IQ参考手册中显示了以下使用的功能;建议的解决方案在我的SQLAnywhere数据库中工作;最终结果是,以下内容应该在IQ中起作用…>>crossing fingersOP的问题提到“更新表1的关键是所有其他列”。以下是根据我写的答案(你这个忍者!:P):
t1.numseq=t2.numseq和t1.Article=t2.Article和t1.ID=t2.ID和t1.numm1=t2.numm1和t1.numm2=t2.Num2和t1.Num4=t2.Num4和t1.Col08=t2.Col08
@RobbieToyota,(我想)Gordon试图指出的一点是,我们正在处理3组相同的联接列值(表1和表2),最终结果是表2中的每一行将与表1中的所有3行联接(即笛卡尔积);那么,如何确保9个连接执行所需的3个更新呢?OP的问题提到“更新Table1的关键是所有其他列”。以下是根据我写的答案(你这个忍者!:P):
t1.numseq=t2.numseq和t1.Article=t2.Article和t1.ID=t2.ID和t1.numm1=t2.numm1和t1.numm2=t2.Num2和t1.Num4=t2.Num4和t1.Col08=t2.Col08
@RobbieToyota,(我想)Gordon试图指出的一点是,我们正在处理3组相同的联接列值(表1和表2),最终结果是表2中的每一行将与表1中的所有3行联接(即笛卡尔积);那么,如何确保9个连接执行所需的3个更新呢?“对于表2中的每一行,连接到表1中的任何一行”:就是这样。我并不在乎表2中的哪一个更新了表1中的哪一个,因为结果是一样的。我无法执行删除/插入操作,因为我在表1中的其他列也都是相同的,但在表2中没有。是的,我想你会带着对表中其他列的评论回来;假设您有权创建临时表。。。我正在写另一个答案,这个答案基于“临时表”,应该(敲敲木头)完成这项工作。。。应该在接下来的5-10分钟内发布…我确实有可能创建临时表“对于表2中的每一行,连接到表1中的任何一行”:就是这样。我并不在乎表2中的哪一个更新了表1中的哪一个,因为结果是一样的。我无法执行删除/插入操作,因为我在表1中的其他列也都是相同的,但在表2中没有。是的,我想你会带着对表中其他列的评论回来;假设您有权创建临时表。。。我正在写另一个答案,这个答案基于“临时表”,应该(敲敲木头)完成这项工作。。。应该在未来5-10分钟内发布…我确实有可能创建临时表这似乎是我所需要的。我现在没有访问数据库的权限,我明天再试。这似乎是我需要的。我现在没有访问数据库的权限,我明天再试。
update  Table1

set     Table1.Col07 = #t2.Col07

from    Table1
join    #t1

        -- rowid() is unique and thus the only 'column' we need in the join between Table1 and itself (aka #t1)

on      rowid(Table1) = #t1.rid

        -- and the rest of the join columns between Table1 and #t1; optional 
/*
and     Table1.numseq      = #t1.numseq
and     Table1.Article     = #t1.Article
and     Table1.ID          = #t1.ID
and     Table1.Num1        = #t1.Num1
and     Table1.Num2        = #t1.Num2
and     Table1.Num3        = #t1.Num3
and     Table1.Num4        = #t1.Num4
and     Table1.Col08       = #t1.Col08
*/

join    #t2

        -- join on our number() values

on      #t1.nbr         = #t2.nbr

        -- and the rest of the join columns between #t1 and #t2; optional but
        -- provides an extra safety check to make sure we don't update records
        -- with the wrong values (eg, number() is generated in wrong order)

and     #t1.numseq      = #t2.numseq
and     #t1.Article     = #t2.Article
and     #t1.ID          = #t2.ID
and     #t1.Num1        = #t2.Num1
and     #t1.Num2        = #t2.Num2
and     #t1.Num3        = #t2.Num3
and     #t1.Num4        = #t2.Num4
and     #t1.Col08       = #t2.Col08
go

select * from Table1
go

 numseq      Article ID          Num1        Num2        Num3        Num4        Col07      Col08
 ----------- ------- ----------- ----------- ----------- ----------- ----------- ---------- -----------
        3023 F         242840794           4          12           1          13 RCN031               9
        3023 F         242840794           4          12           1          13 RCN035               9
        3023 F         242840794           4          12           1          13 RCN037               9