'SELECT the current value of a column based on date of another table
I have 4 tables:
- INVOICE, contains the entry date.
- INVOICE_ITEM contains the product cod's.
- PRODUCTS, here contains the STANDARD column, It shows only the current standard value.
- PRODUCT_HIST (contains the standard value changes of the products table).
I'm trying to select the "standard" value (Can be 'Y' or 'N') of the product_hist based on the invoice entry date.
My select below isn't showing the correct values
SELECT NT.INVOICE
, NT.PROVIDER
, NT.ENTRY
, NTI.PRODUCT
, P.COD
, P.NAME
, Nvl(P.STANDARD, 'Y') "STANDARD_ACTUAL"
, COALESCE ((SELECT To_Char(MAX(PH.DATE)) FROM PRODUCT_HIST PH
WHERE PH.COD = P.COD),
P.STANDARD, 'S') "STANDARD2"
FROM INVOICE NT
INNER JOIN INVOICE_ITEM NTI ON NT.NOTE = NTI.NOTE AND NT.PROVIDER = NTI.PROVIDER
INNER JOIN PRODUCTS P ON P.COD = NTI.PRODUCT
WHERE NT.ENTRY BETWEEN TO_DATE('01/01/2019','DD/MM/YYYY') AND TO_DATE('31/12/2019','DD/MM/YYYY')+0.99999
I wanna show the current value when the invoice had an entry (STANDARD2)
In the COALESCE column I have the last value, it's not based on the invoice entry
I'm 3 days on it and cant make it work. Would be grateful if someone can help me, please.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
