H
H
hatorihanso2020-06-05 11:03:28
Oracle
hatorihanso, 2020-06-05 11:03:28

How to correctly refer to the next row in the table using the LEAD function?

There is a PRODUCTINFO table with two columns: PRODUCTID (NUMBER) and ALLOWFROM (DATE). I need to insert a new ALLOWTO (DATE) row into a table that is equal to the next ALLOWFROM entry minus 1 day. If there is no next ALLOWFROM entry for a particular PRODUCTID, insert 01-01-1900.

I am using this script but the result is not correct.

SELECT PRODUCTID, ALLOWFROM,
    LEAD(ALLOWTO -1, 1, '01-01-1900') OVER (ORDER BY ALLOWFROM ASC)  AS ALLOWTO
    FROM PRODUCTINFO
    ORDER BY PRODUCTID, ALLOWFROM;


q0px2.png

What am I doing wrong? Where is the mistake?

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hatorihanso, 2020-06-05
@hatorihanso

Issue resolved. Must use OVER (PARTITION BY ITEM_ID ORDER BY ALLOWFROM ASC)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question