I need to join two tables, the first one presenting all the records contained in it, with the condition that what is in the second table has a lower price with data_ini
and dt_fim
using current_date
to define if it is inside of the condition that is, within the current date.
Example:
Table 1:
---------------+------------+------
cod_barras | descrição | preco
---------------+------------+------
00000000000017 | PAO DE SAL | 0,50
00000000000122 | PEPSI 1 5L | 5,00
00000000000987 | TAIOBA | 2,00
00000000001526 | ALHO KG | 20,00
Table 2:
---------------+-------+------------+-----------
barras | preco | DT_ini | DT_FIM
---------------+-------+------------+-----------
00000000000017 | 0,30 | 03/11/2017 | 10/11/2017
00000000001526 | 15,00 | 05/11/2017 | 08/11/2017
Considering that current_date
= 04/11/2017, should look like this:
---------------+------------+------
cod_barras | descrição | preco
---------------+------------+------
00000000000017 | PAO DE SAL | 0,30
00000000000122 | PEPSI 1 5L | 5,00
00000000000987 | TAIOBA | 2,00
00000000001526 | ALHO KG | 20,00
Considering that current_date
= 07/11/2017, should look like this:
---------------+------------+------
cod_barras | descrição | preco
---------------+------------+------
00000000000017 | PAO DE SAL | 0,30
00000000000122 | PEPSI 1 5L | 5,00
00000000000987 | TAIOBA | 2,00
00000000001526 | ALHO KG | 15,00
Considering that current_date
= 11/11/2017, should look like this:
---------------+------------+------
cod_barras | descrição | preco
---------------+------------+------
00000000000017 | PAO DE SAL | 0,50
00000000000122 | PEPSI 1 5L | 5,00
00000000000987 | TAIOBA | 2,00
00000000001526 | ALHO KG | 20,00
That is, I need an item that exists in Table 2 within a specific date to pick up the price from Table 2 and not from Table 1.
It will take the value of table 1, if it is not contained in table 2 and within the current date.
Who can help, is a bit difficult.