0
I need to return the last two purchases of each table item NOTA_FISCAL_ENTRADA_ITEM
, but returns column values VL_UNITARIO_ENTRADA_EST
different.
That is, if in the last two purchases the value is the same, fetch the next most recent purchase with the different value.
In the SELECT
below, I managed to return only the two most recent purchases.
How do I bring the last two purchases with different values? I tried with the DISTINCT
, but without success.
SELECT *
from (
SELECT CD_ITEM,
DT_ENTRADA,
VL_UNITARIO_ENTRADA_EST,
row_number () over (partition by CD_ITEM order by DT_ENTRADA DESC) ULT_REG
FROM NOTA_FISCAL_ENTRADA_ITEM
)
WHERE ULT_REG <= 2
Current SELECT output:
" That is, if in the last two purchases the value is the same, fetch the next most recent purchase with the different value" this is a little difficult to do without having an example of the data, but if using a
group by
including the value field, and aorder by
to ensure that you sort and take the next different value, you must solve– Ricardo Pontual