开发者

Replace null with known value with same id

开发者 https://www.devze.com 2023-03-10 08:31 出处:网络
How can I do that in a optimal way? TABLE开发者_开发知识库 ID FLAG VALUE ----------------------------

How can I do that in a optimal way?

TABLE开发者_开发知识库

ID FLAG VALUE
----------------------------
1   Y   52
1   N   NULL
2   Y   51
3   N   54

OUTPUT

ID FLAG VALUE
----------------------------
1   Y   52
1   N   52
2   Y   51
3   N   54


UPDATE 
  theTable
SET 
  value = (SELECT MAX(value) FROM theTable i WHERE i.id = theTable.id)
WHERE
  value IS NULL


you can do it in two ways

SELECT Id,
       Flag, 
       MAX(Value) OVER(PARTITION BY Id) CValue
FROM   Table_1

or

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
0

精彩评论

暂无评论...
验证码 换一张
取 消