0

有关示例数据,请参阅下面的屏幕截图。规范化这些数据我发现这些相似的代码就我的数据而言意味着相同的事情。我正在尝试更新我的表格,以便primarycode将右侧的 s 更新为左侧的

在此处输入图像描述

我用来查找这些值的查询:

select  *
from icd
inner join icd as i2 on i2.primarycodetext=icd.primarycodetext
and i2.primarycode <> icd.primarycode
where icd.primarycode like '%0'

我试过的更新查询。

update icd
set icd.primarycode=i2.primarycode
from 
(


select  *
from icd
inner join icd as i2 on i2.primarycodetext=icd.primarycodetext
and i2.primarycode <> icd.primarycode
where icd.primarycode like '%0'
)t

收到的错误:Msg 8156, Level 16, State 1, Line 12 The column 'primarycode' was specified multiple times for 't'. 编辑:此表中仅有的两列是 primarycode 和 primarycodetext

4

1 回答 1

2

好的,对于您的问题,您应该执行以下操作:

UPDATE I1
SET I1.primarycode = I2.primarycode
FROM icd AS I1
INNER JOIN icd AS I2 
ON i2.primarycodetext = i1.primarycodetext
AND i2.primarycode <> I1.primarycode
WHERE I1.primarycode LIKE '%0'

但是这样做之后你的桌子上会有重复的。考虑一下您真正想要获得的内容(这些值的唯一记录),那么您应该使用DELETE这些记录而不是更新它们:

DELETE I1
FROM icd AS I1
INNER JOIN icd AS I2 
ON i2.primarycodetext = i1.primarycodetext
AND i2.primarycode <> I1.primarycode
WHERE I1.primarycode LIKE '%0'
于 2012-07-30T20:03:10.713 回答