在Teradata中,如果存在重复记录,如何仅更新一条记录?

时间:2014-04-21 22:10:42

标签: sql teradata

当我执行以下SELECT语句时:

SELECT startdate
      ,enddate
      ,invoicenumber
      ,vendornumber
      ,upc
      ,store
      ,cost
      ,allowance
      ,reason
      ,row_number() over(partition by upc order by startdate desc) as rownum 
FROM db.table
WHERE  StartDate=DATE'2014-01-01' 
   AND EndDate=DATE'2014-01-01' 
   AND InvoiceNumber IS NULL 
   AND VendorNumber='2505' 
   AND UPC='1234568' 
   AND Store IS NULL 
   AND Cost=1.01 
   AND Allowance IS NULL

我得到2个结果: results

如何仅更新第一行的UPC?

2 个答案:

答案 0 :(得分:0)

简短回答:你不能(除非你的系统启用了ROWID,但概率非常低)。

但您可以将行插入易失性表格,删除并从VT重新插入。

顺便说一句,这张桌子没有主键吗?

答案 1 :(得分:0)

您可以通过3个步骤解决问题

--1) create volatile table with one row you need
create MULTISET  volatile table  t_tc_temp as 
(SELECT startdate ,enddate ,invoicenumber ,vendornumber
      ,upc ,store ,cost ,allowance,reason
 FROM db.table
WHERE  StartDate=DATE'2014-01-01' 
   AND EndDate=DATE'2014-01-01' 
   AND InvoiceNumber IS NULL 
   AND VendorNumber='2505' 
   AND UPC='1234568' 
   AND Store IS NULL 
   AND Cost=1.01 
   AND Allowance IS NULL  
   group by 1,2,3,4,5,6,7,8,9)
   with data NO PRIMARY INDEX ON COMMIT PRESERVE ROWS;
--  2) delete duplicated rows
 delete from db.table where  -- .... yor  conditions

--- 3) insert one row from volatile tabel
    insert into db.table 
    (select t.* from t_tc_temp t 
    -- insert updated row  for example
    union           
     SELECT startdate ,enddate ,invoicenumber ,vendornumber
          ,1111 ,store ,cost ,allowance,reason from t_tc_temp t2 )

P.S。 或者您可以使用SET创建Primary Index易失性表,而不使用group by,因为set表检查重复项