Cześć,
opis w temacie może nie najlepszy ale nie wymyśliłem nic mądrzejszego. Problem polega na połączeniu 2 tablic gdzie z nie wiadomego powodu jest rozjazd na czasie [TransactionTime] i w jednej z tablic ta kolumna posiada wartość albo o sekundę mniejszą bądź większą. Pytanie brzmi czy jest jakiś mądrzejszy/sprytniejszy/przejrzystszy sposób na poniższy kod ?
Tym bardziej, że na tą chwile wiemy, że to tylko sekunda ale kto wie co się może zdarzyć w przyszłości.
...
LEFT JOIN Baan..InventoryTransactionsByItem it
ON pd.TransactionDate = it.TransactionDate
AND pd.TransactionTime = it.TransactionTime
AND pd.OrderNumber = it.OrderNumber
AND pd.OrderLine = it.OrderLineNumber
AND pd.Warehouse = it.Warehouse
-- Due to issue with time difference for some records and 1 second difference we MINUS 1 sec in Inv. Trans. table
LEFT JOIN Baan..InventoryTransactionsByItem it1
ON pd.TransactionDate = it1.TransactionDate
AND pd.TransactionTime = it1.TransactionTime - 1 -- << pomniejszam czas o sec
AND pd.OrderNumber = it1.OrderNumber
AND pd.OrderLine = it1.OrderLineNumber
AND pd.Warehouse = it1.Warehouse
-- Due to issue with time difference for some records and 1 second difference we ADD 1 sec in Inv. Trans. table
LEFT JOIN Baan..InventoryTransactionsByItem it2
ON pd.TransactionDate = it2.TransactionDate
AND pd.TransactionTime = it2.TransactionTime + 1 -- << podwyższam czas o sec
AND pd.OrderNumber = it2.OrderNumber
AND pd.OrderLine = it2.OrderLineNumber
AND pd.Warehouse = it2.Warehouse
Po tak podłączonych tabelach potrzebną wartość wybieram korzystając z COALESCE()
COALESCE(it.Quantity, it1.Quantity, it2.Quantity)
Z góry dzięki za podpowiedzi.