Here I am using Oracle SQL and I have a table with 2 columns, Keyword
and Created_Date
.
Is there any way to get the 3rd column with information of next entry of 2nd column in accordance with first column?
Thanks guys
Advertisement
Answer
Looks like the LEAD
analytic function. Sample data in lines #1 – 10; query begins at line #11.
SQL> with test (keyword, datum) as 2 (select 'A', date '2021-01-18' from dual union all 3 select 'A', date '2021-04-26' from dual union all 4 select 'B', date '2021-03-01' from dual union all 5 select 'B', date '2021-04-26' from dual union all 6 select 'B', date '2021-03-01' from dual union all 7 select 'C', date '2021-02-24' from dual union all 8 select 'C', date '2021-02-24' from dual union all 9 select 'C', date '2021-08-04' from dual 10 ) 11 select keyword, 12 datum, 13 lead(datum) over (order by keyword, datum) next_entry_date 14 from test 15 order by keyword, datum; KEYWORD DATUM NEXT_ENTRY -------- ---------- ---------- A 18.01.2021 26.04.2021 A 26.04.2021 01.03.2021 B 01.03.2021 01.03.2021 B 01.03.2021 26.04.2021 B 26.04.2021 24.02.2021 C 24.02.2021 24.02.2021 C 24.02.2021 04.08.2021 C 04.08.2021 8 rows selected. SQL>