SQL -Update Table2包含Table1中的数据,但不更新表2中的精确重复

时间:2017-09-05 15:41:25

标签: sql-server

[涉及2个表]我需要从TABLE1获取Tracking#并将该值提供给TABLE2中的Tracking#列,即匹配记录。虽然,我只需要对TABLE2中不重复的记录执行此操作。这意味着TABLE2中的第2行和第3行不需要更新(因为它们完全相同),但只需要更新第1行和第4行。我如何获得?
还有其他列,但我没有包含它们以使示例更清晰。您在下面的查询中看到的其他字段是使记录唯一的字段。首先,我不确定我写的查询是否正确,因为我在解决这个问题时遇到了问题。其次,选择正在运行,它正在选择所有指示的值匹配的跟踪号,但是当我包含更新时,我收到错误:

  

(“Msg 116,Level 16,State 1,Line 24只有一个表达式   在未引入子查询时在选择列表中指定   EXISTS”)。

我真的很感激一些帮助,谢谢

TABLE1

|Pro #| Dept|Amount  |Tracking # |  
|-----|-----|--------|-----------|  
|___2 | _35 |______5 |  ______32 |  
|___2 | _35 |_____10 |  ______35 |  
|___2 | _35 |_____10 |  ______12 |  
|___2 | _35 |______8 |  ______23 |

TABLE2

|Pro #| Dept  | Amount |Tracking # |  
|-----|-------|--------|-----------|  
|___2 | _35   | ______5| YES, it needs tracking #  
|___2 | _35   | _____10| NO, does not need tracking #  
|___2 | _35   | _____10| NO, does not need tracking #   
|___2 | _35   | ______8| YES, it needs tracking #

代码:

UPDATE FinalTbl
SET [TrackingNumber] = TempTbl.[TrackingNumber]
FROM [FedPayHistory].[dbo].[Frt_FinalNon000TEST] FinalTbl
INNER JOIN
(Select [Source]
  ,[WkEndDt]
  ,[Yr]
  ,[Season]
  ,[Per]
  ,[Wk]
  ,[Div]
  ,[ProNum]
  ,[ApptNum]
  ,[Dept]
  ,[Vend]
  ,[AllocAmt]
  ,[RTV]
  ,[KeyRec]
  ,[BatchNum]
  ,[TrackingNumber]
  ,[MatchCode]
  ,count(*) as CNT
     FROM [FedPayHistory].[dbo].[Frt_FinalNon000TEST]
     GROUP BY [Source],[WkEndDt],[Yr],[Season],[Per],[Wk],[Div],[ProNum],[ApptNum],[Dept],[Vend],[AllocAmt],[RTV],[KeyRec],[BatchNum],[TrackingNumber],[MatchCode]
     HAVING count(*)<2) JoinTable
  ON 
  FinalTbl.[Source] = JoinTable.[Source] and
  FinalTbl.[Yr] = JoinTable.[Yr] and 
  FinalTbl.[Season] = JoinTable.[Season] and
  FinalTbl.[Per] = JoinTable.[Per] and
  FinalTbl.[Wk] = JoinTable.[Wk] and
  FinalTbl.[Div] = JoinTable.[Div] and
  FinalTbl.[Dept] = JoinTable.[Dept] and 
  FinalTbl.[Vend] = JoinTable.[Vend] and
  FinalTbl.[KeyRec] = JoinTable.[KeyRec] and
  FinalTbl.[RTV] = JoinTable.[RTV] and
  FinalTbl.[AllocAmt] = JoinTable.[AllocAmt] and
  FinalTbl.[ProNum] = JoinTable.[ProNum] 

 INNER JOIN [FedPayHistory].[dbo].[Frt_tempNon000] TempTbl 
 ON FinalTbl.Source = TempTbl.Source and
 FinalTbl.[Yr] = TempTbl.[Yr] and 
 FinalTbl.[Season] = TempTbl.[Season] and
 FinalTbl.[Per] = TempTbl.[Per] and
 FinalTbl.[Wk] = TempTbl.[Wk] and
 FinalTbl.[Div] = TempTbl.[Div] and
 FinalTbl.[Dept] = TempTbl.[Dept] and 
 FinalTbl.[Vend] = TempTbl.[Vend] and
 FinalTbl.[KeyRec] = TempTbl.[KeyRec] and
 FinalTbl.[RTV] = TempTbl.[RTV] and
 FinalTbl.[AllocAmt] = TempTbl.[AllocAmt] and
 FinalTbl.[ProNum] = TempTbl.[ProNum] and
 JoinTable.[Source] = TempTbl.[Source] and
 JoinTable.[Yr] = TempTbl.[Yr] and 
 JoinTable.[Season] = TempTbl.[Season] and
 JoinTable.[Per] = TempTbl.[Per] and
 JoinTable.[Wk] = TempTbl.[Wk] and
 JoinTable.[Div] = TempTbl.[Div] and
 JoinTable.[Dept] = TempTbl.[Dept] and 
 JoinTable.[Vend] = TempTbl.[Vend] and
 JoinTable.[KeyRec] = TempTbl.[KeyRec] and
 JoinTable.[RTV] = TempTbl.[RTV] and
 JoinTable.[AllocAmt] = TempTbl.[AllocAmt] and
 JoinTable.[ProNum] = TempTbl.[ProNum] 

1 个答案:

答案 0 :(得分:0)

你需要这样的东西:

UPDATE t2
SET [TrackingNumber] = t1.[TrackingNumber]
FROM [TABLE2] t2
INNER JOIN
  (SELECT ProNum,
        Dept,
        AllocAmt,
        Count(*) AS CNT
   FROM [TABLE2]
   GROUP BY ProNum,
            Dept,
            AllocAmt 
   HAVING count(*)<2
) a
 ON t2.AllocAmt =a.AllocAmt AND 
    t2.ProNum = a.ProNum AND 
    t2.dept = a.dept
INNER JOIN [TABLE1] t1 
ON t2.[Source] = t1.[Source] AND
 t2.[Yr] = t1.[Yr] AND
 t2.[Season] = t1.[Season] AND
 t2.[Per] = t1.[Per] AND
 t2.[Wk] = t1.[Wk] AND
 t2.[Div] = t1.[Div] AND
 t2.[Dept] = t1.[Dept] AND
 t2.[Vend] = t1.[Vend] AND
 t2.[KeyRec] = t1.[KeyRec] AND
 t2.[RTV] = t1.[RTV] AND
 t2.[AllocAmt] = t1.[AllocAmt] AND
 t2.[ProNum] = t1.[ProNum] AND
 a.[Dept] = t1.[Dept] AND
 a.[AllocAmt] = t1.[AllocAmt] AND
 a.[ProNum] = t1.[ProNum]