Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Substituir valor nulo pelo valor mais recente


Isso funcionará para o seu caso, eu acho:
SELECT 
    t1.PositionDate,
    t1.Currency,
    COALESCE(t1.Value,t2.value) AS Value
FROM t1
LEFT join (SELECT MAX(PositionDate) AS PositionDate,Currency FROM t2 WHERE PositionDate < t1.PositionDate  GROUP BY Currency) tjoin
LEFT join t2 on tjoin.currency = t2.Currency and tjoin.PositionDate = t2.PositionDate