我在SQL Server上有这样的表:
code description percentage
123 abc 1
123 oke 0
123 cfd 0
234 kde 2
234 kfc 0
234 kfc 0
如何将每个代码组的所有“0百分比”记录的描述更新为非零百分比记录?例如我追求的结果是:
code description percentage
123 abc 1
123 abc 0
123 abc 0
234 kde 2
234 kde 0
234 kde 0
答案 0 :(得分:2)
Update T1 set description = T2.Description from YourTable T1
inner join
( select code, description from yourTable group by code, description
where percentage <> 0 ) T2 on T1.Code = T2.Code
where T1.Code = 0
<强>被修改强> 这考虑到每个组仅对另一个描述只有零。
答案 1 :(得分:1)
尝试
UPDATE Table
SET description=(SELECT TOP 1 description
FROM Table t
WHERE t.code = Table.code AND percentage<>'0')
WHERE percentage='0'