1
I need to join two tables, being the first 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 the current_date
to define whether this is within the condition or 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
whereas the 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
whereas the 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
whereas the 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 the price from Table 2 and not from Table 1.
You will get the value of table 1, if it is not contained in table 2 and within the current date.
Who can help, it’s a little difficult.
Can you pass this in SQL Fiddle ? http://sqlfiddle.com/ So we could simulate it more easily here.
– rbz