[英]select statement to return value from a different row
我有一個返回這些結果的 select 查詢:
Transaction Type Value
----------- ---- -----
1 A Null
1 A Null
1 B 1234
2 A Null
2 A Null
2 B 4321
我將如何形成它,以便 A 類型的 Null 值被 B 類型的值替換? 要得到這個結果:
Transaction Type Value
----------- ---- -----
1 A 1234
1 A 1234
1 B 1234
2 A 4321
2 A 4321
2 B 4321
您可以使用 window 函數:
select transaction, type, value,
max(case when type = 'b' then value end) over (partition by transaction) as new_value
from t;
如果您只想要事務的非NULL
值:
select transaction, type, value,
max(value) over (partition by transaction) as new_value
from t;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.