Along with saying hello and thanks for this space, I ask for help with the following. I have two tables .. the table1. is a historical sales record with the fields fecha_vta, código_producto, cantidad. in the other table I have the variation of costs per product by date with the following fields date_costs, code_product, cost. The idea is that according to the sales history (table 1) look for the cost of the product (table 2) closest or equal to the sales date.
table 1
fecha_vta codigo_producto cantidad
-------- --------- ------------
5/5/2018 prod1 10
6/5/2018 prod2 5
6/5/2018 prod1 8
7/6/2018 prod2 20
15/6/2018 prod1 3
Table 2
fecha_costo codigo_producto costo
---------------- --------------------- -------
01/05/2018 prod1 2
06/05/2018 prod1 2,5
06/05/2018 prod2 3
09/06/2018 prod1 2,8
expected result:
fecha_vta codigo_producto cantidad costo fecha_costo
------------ ---------------------- ----------- ------- ----------------
5/5/2018 prod1 10 2 01/05/2018
6/5/2018 prod2 5 3 06/05/2018
6/5/2018 prod1 8 2,5 06/05/2018
7/6/2018 prod2 20 3 06/05/2018
15/6/2018 prod1 3 2,8 09/06/2018
The idea is that the query brings me the cost of the product closest to the date_ cost to the sales_date. see in the result table that prod1 brings three different costs related to the proximity of the sale date with the date of cost of the product.
The closeness of the date is currently looking for the same or smaller results, but the next date should look for if it is the same, bigger or smaller and the closest one to bring it and show it.
**
on the cost date shows 01/05/2018 but must be 06/05/2018
**
I appreciate your help or advice to solve this problem.