简体   繁体   English

SQL-Oracle:根据同一表中包含的值更新表多行

[英]SQL-Oracle: Updating table multiple row based on values contained in the same table

I have one table named: ORDERS 我有一个表名为:ORDERS

this table contains OrderNumber's which belong to the same person and same address lines for that person. 该表包含属于同一个人的OrderNumber,以及该个人的相同地址行。

However sometimes the data is inconsistent; 但是有时数据不一致。 as example looking at the table screenshot: Orders table with bad data to fix - 例如查看表格屏幕截图: Orders表格中有错误数据要修复 -

you all can noticed that orderNumber 1 has a name associated to and addresses line1-2-3-4. 大家都可以注意到orderNumber 1有一个与之关联的名称,并且地址为行1-2-3-4。 sometimes those are all different by some character or even null. 有时,这些都因某些字符而不同甚至为null。

my goal is to update all those 3 lines with one set of data that is already there and set equally all the 3 rows. 我的目标是使用一组已经存在的数据更新所有这3行,并平等地设置所有3行。

to make more clear the result expected should be like this: 为了更清楚地说明预期的结果应该是这样的:

enter image description here 在此处输入图片说明

i am currently using a MERGE statement to avoid a CURSOR (for loop ) 我目前正在使用MERGE语句来避免CURSOR(for loop)

but i am having problems to make it work 但我在使其工作方面遇到问题

here the SQL 这里的SQL

    MERGE INTO ORDERS O USING
    (SELECT
     INNER.ORDERNUMBER,
      INNER.NAME,
      INNER.LINE1,
      INNER.LINE2,
      INNER.LINE3,
      INNER.LINE4
    FROM ORDERS INNER
      ) TEMP 
ON( O.ORDERNUMBER  = TEMP.ORDERNUMBER  )
WHEN MATCHED THEN
  UPDATE
  SET 
          O.NAME = TEMP.NAME,
          O.LINE1 = TEMP.LINE1,
          O.LINE2 = TEMP.LINE2,
          O.LINE3 = TEMP.LINE3,
          O.LINE4 = TEMP.LINE4;

the biggest issues i am facing is to pick a single row out of the 3 randomly - it does not matter whihc of the data - row i pick to update the line/s as long i make the records exaclty the same for an order number. 我面临的最大问题是从3条中随机选择一行-数据的大小无关紧要-只要我使记录的订货号完全相同,我就选择更新行。

i also used ROWNUM =1 but it in multip[le updates will only output one row and update maybe thousand of lines with the same address and name whihch belong to an order number. 我还使用了ROWNUM =1但在multip [le更新中将仅输出一行,并更新可能具有相同地址和名称的几千行属于订单号。

order number is the join column to use ... 订单号是要使用的联接列...

kind regards 亲切的问候

A simple correlated subquery in an update statement should work: update语句中的一个简单的相关子查询应该可以工作:

update orders t1
   set (t1.name, t1.line1, t1.line2, t1.line3, t1.line4) =
          (select t2.name, t2.line1, t2.line2, t2.line3, t2.line4
             from orders t2
            where t2.OrderNumber = t1.OrderNumber
              and rownum < 2)

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

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