我怎样才能以最佳方式做到这一点?
表
ID FLAG VALUE
----------------------------
1 Y 52
1 N NULL
2 Y 51
3 N 54
输出
ID FLAG VALUE
----------------------------
1 Y 52
1 N 52
2 Y 51
3 N 54
答案 0 :(得分:3)
UPDATE
theTable
SET
value = (SELECT MAX(value) FROM theTable i WHERE i.id = theTable.id)
WHERE
value IS NULL
答案 1 :(得分:1)
你可以用两种方式做到这一点
SELECT Id,
Flag,
MAX(Value) OVER(PARTITION BY Id) CValue
FROM Table_1
或
select X.Id, X.Flag,
case when ISNULL(X.Value,0) = 0 then
(select MAX(A.Value) from Table_1 A where A.Id = X.Id)
else X.Value end
from Table_1 X